Analysis of the integration process of springboot with swagger

Time:2020-11-21

This article mainly introduces the integration of springboot swagger process analysis, the article through the example code is very detailed, for everyone’s study or work has a certain reference learning value, need friends can refer to

Springboot integrated with swagger

1、 pom.xml To introduce:


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

2. Configuration class:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
  @Bean
  public Docket swaggerSpringMvcPlugin() {

    return new Docket(DocumentationType.SWAGGER_2)
        .select()
        //Only the classes annotated with apioperation can generate interface documents
        .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
        .build();
  }

}

3. Corresponding notes of controller: @ apioperation

@Apioperation (value = user login, notes =)
  @PostMapping("/loginOn")
  public ResponseMessage loginOn(@RequestBody @Valid UserReq userReq){
    ResponseMessage responseMessage = userServiceImp.loginOn(userReq);
    return responseMessage;
  }

Last local default access: http://localhost :8080/swagger- ui.html

You can see the effect picture of relevant interfaces

Possible reasons for page access failure:

1》 The access method is originally 404 error: in sprigboot, there is an important concept called: Convention is better than configuration

When springboot is started, if no package path is specified for scanning, the current package and the components under the sub package will be loaded by default. Note here

If you put the startup class into the service package, the page will not be able to access:

2》 There is a problem with the writing of swaggerconfig class: there are many dock methods. Here we need to pay attention to the following:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
  @Bean
  public Docket swaggerSpringMvcPlugin() {

    return new Docket(DocumentationType.SWAGGER_2)
        .select()
        //Only the classes annotated with apioperation can generate interface documents
        .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
        .build();
  }

}

3》 Whether the interceptor is intercepted when the interceptor is configured:

When we implement the webmvcconfigurer interface, when we configure the interceptor, we need to filter and release the corresponding requests, such as static resources, login requests, etc

@Configuration
public class WebConfig implements WebMvcConfigurer {
  /**
   *Configure interceptors
   * @param registry
   */
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new RequestInterceptor()).addPathPatterns("/**").excludePathPatterns("/user/login")
        //Exclude swagger
    .excludePathPatterns("/swagger-resources/**", "/webjars/**",
        "/v2/**", "/swagger-ui.html/**");
  }

}

Some codes are written by rewriting the addresourcehandlers method of webmvcconfigurer

/**
   *Add static resources -- filter swagger API (open source online API document)
   * @param registry
   *//*
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    //Filter swagger
    registry.addResourceHandler("swagger-ui.html")
        .addResourceLocations("classpath:/META-INF/resources/");

    registry.addResourceHandler("/webjars/**")
        .addResourceLocations("classpath:/META-INF/resources/webjars/");

    registry.addResourceHandler("/swagger-resources/**")
        .addResourceLocations("classpath:/META-INF/resources/swagger-resources/");

    registry.addResourceHandler("/swagger/**")
        .addResourceLocations("classpath:/META-INF/resources/swagger*");

    registry.addResourceHandler("/v2/api-docs/**")
        .addResourceLocations("classpath:/META-INF/resources/v2/api-docs/");

  }*

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.