swagger3.0 (springboot) eliminate basic-error-controller

created at 08-23-2021 views: 45

1. To create a new springboot project, you can quickly generate a springboot project through https://start.spring.io/.

2. Introduce jar dependency:

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-boot-starter</artifactId>
  <version>3.0.0</version>
</dependency>

3. Write Controller and configure corresponding annotations, see below

package com.demo.incubator.swaggerdemo.controller;

import java.util.Map;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/demo")
@Api(protocols = "http,https", produces = "application/json", tags = "mock样例")
public class SwaggerDemoController {
     @PostMapping("/mokeInvoke")
     @ApiOperation(value = "mock method example", notes = "return a string")
     public String mockInvoke(@ApiParam(value = "displayed information", required = true) @RequestParam("showMsg") String showMsg) {
         return showMsg;
     }

    @PostMapping("/mokeInvoke2")
   @ApiOperation(value = "mock method example 2", notes = "return a string")
     @ApiImplicitParams({@ApiImplicitParam(value = "phone number", name = "userPhone", required = true, paramType = "query"),
             @ApiImplicitParam(value = "name", name = "userName", required = true, paramType = "query"),
             @ApiImplicitParam(value = "address", name = "address", required = true, paramType = "query"),
             @ApiImplicitParam(value = "system source", name = "appid", required = true, paramType = "header")})
     public String mockInvoke2(@RequestBody Map<String, Object> params) {
         return JSON.toJSONString(params);
    }
}

4. Start the project, visit the link address http://localhost:8080/swagger-ui/index.html (replace the port 8080 with the port configured by yourself), you can see the swagger page, as shown below

Start the project

5. But you will find that there are more basic-error-controller, operation-handler, web-mvc-links-handler, which can be eliminated by creating a new Configration, and only the interface we provide externally

package com.demo.incubator.swaggerdemo.config;

import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class SwaggerConfiguration {    
    @Bean
    Docket xiaokeai() {
        return new Docket(DocumentationType.OAS_30).useDefaultResponseMessages(false)
                .produces(Stream.of("application/xml", "application/json").collect(Collectors.toSet())).select()
                .paths(PathSelectors.regex("/demo/.*")).build()
                .protocols(Stream.of("http", "https").collect(Collectors.toSet()));
    }
}
  • Note that the /demo/ must be able to match all your interface paths. For example, in my example, both interfaces start with demo, /demo/mockInvoke and /demo/mockInvoke2.

6. Start the project again, and you will find that only the interface exposed by your own controller is retained.

created at:08-23-2021
edited at: 08-23-2021: