SpringBoot integrates Swagger to build API documents

Meven configuration

<!-- Swagger -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
</dependency>

Register components

@Configuration
@EnableSwagger2
public class Swagger2Configuration {

    @Bean
    public Docket accessToken() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())//Configuration description
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zsdk.controller"))//Blocked package
                .paths(PathSelectors.any())//The intercepted interface path can also be specified, such as: regex("/v1/user /. *")
                .build();

    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot utilize Swagger structure API File")
                .description("Interface documentation")
                .termsOfServiceUrl("http://www.kris.com")
                .contact(new Contact("kris", "http://Www.zsdk. Com "," 123456789 @ QQ. Com ") / / contact
                .version("1.0")
                .build();
    }

}

Configure static file mapping

/**
 * Interceptor configuration
 *
 * @author kris
 * @create 2018-01-08 11:45
 */
@Configuration
public class ServletContextConfig extends WebMvcConfigurationSupport {

    /**
     *  It is found that if WebMvcConfigurationSupport is inherited, the related content configured in yml will be invalid.
     *  Static resources need to be reassigned
     *  @param registry
     * 
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

    /**
     * Configure servlet processing
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

Write controller

/**
 * @author kris
 * @create 2017-10-11 17:45
 */
@RestController
@RequestMapping("/v1/basic")
public class BasicController extends WebApiController{

    @Autowired
    private CustomConfigService mCustomConfigService;   

    @PostMapping("/init")
    public WebApiResult init(@RequestBody InitReqModel model) {


        //Obtain initialization configuration, such as customer service information, official website
        CustomServiceConfig serviceConfig = mCustomConfigService.findCustomConfig(model.getAppId(),model.getChannelId());

        return apiSuccess(serviceConfig);
    }
} 

View effect

  1. Start the app at: http://localhost:8080/swagger-ui.html
  2. design sketch

Custom (use of annotations)

@ApiIgnore
Ignore exposed APIs

@ApiOperation(value = find, notes = find user based on user ID)
Add description

Other notes:

@Api: used on a class to illustrate its function

@Apiimplicit params: used to include a set of parameter descriptions on Methods

@ApiResponses: used to represent a set of responses

@ApiResponse: used in @ ApiResponses, generally used to express a wrong response information

code: number, for example 400
Message: message, such as "request parameter not filled in"
response: the class throwing the exception

@ApiModel: describes the information of a Model (this kind of information is generally used in the field of @ RequestBody when post is created
Jing, when the request parameter cannot be described with @ ApiImplicitParam annotation)

@ApiModelProperty: describes the properties of a model

Tags: SpringBoot

Posted on Tue, 05 May 2020 18:00:50 -0400 by Plagel