美文网首页
JAVA-springboot集成swagger

JAVA-springboot集成swagger

作者: 坚持也是一种成功 | 来源:发表于2023-03-16 00:31 被阅读0次

    1、版本说明

      springboot 2.1.6+swagger 2.9.2
    

    2、maven引入

       <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>
    

    3、代码实现

    3.1 单个api管理模式

     import io.swagger.annotations.Api;
     import org.springframework.context.annotation.Bean;
     import org.springframework.context.annotation.Configuration;
     import springfox.documentation.builders.ApiInfoBuilder;
     import springfox.documentation.builders.PathSelectors;
     import springfox.documentation.builders.RequestHandlerSelectors;
     import springfox.documentation.service.ApiInfo;
     import springfox.documentation.spi.DocumentationType;
     import springfox.documentation.spring.web.plugins.Docket;
     import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
     @Configuration//配置文件
     @EnableSwagger2//插件
     public class SwaggerConfig  {
          @Value("${spring.profiles.active}")
          private String active;//获取当前yml文件中启动环境
          
          @Bean
          public Docket createRestApi() {
              return new Docket(DocumentationType.SWAGGER_2)
                        .groupName("网关服务接口文档")//设置名称
                        .enable(!"prd".equals(active))//设置发布环境不允许访问swagger
                        .apiInfo(apiInfo()).enable(true)
                        .select()
                        .apis(RequestHandlerSelectors.basePackage("com.controller.app"))//扫描api的路径
                        .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                        .paths(PathSelectors.any())
                        .build();
          }
    
    
    
          private ApiInfo apiInfo() {
              return new ApiInfoBuilder()
                    .title("网关服务接口文档")
                    .description("网关服务接口文档")
                    .version("1.0")
                    .build();
         }
    }
    

    3.2 多个api管理模式

     import io.swagger.annotations.Api;
     import org.springframework.context.annotation.Bean;
     import org.springframework.context.annotation.Configuration;
     import springfox.documentation.builders.ApiInfoBuilder;
     import springfox.documentation.builders.PathSelectors;
     import springfox.documentation.builders.RequestHandlerSelectors;
     import springfox.documentation.service.ApiInfo;
     import springfox.documentation.spi.DocumentationType;
     import springfox.documentation.spring.web.plugins.Docket;
     import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
     @Configuration//配置文件
     @EnableSwagger2//插件
     public class SwaggerConfig  {
          @Value("${spring.profiles.active}")
          private String active;//获取当前yml文件中启动环境
          
          @Bean
          public Docket createRestApi() {
              return new Docket(DocumentationType.SWAGGER_2)
                        .groupName("网关服务接口文档")//设置名称
                        .enable(!"prd".equals(active))//设置发布环境不允许访问swagger
                        .apiInfo(apiInfo()).enable(true)
                        .select()
                        .apis(RequestHandlerSelectors.basePackage("com.controller.app"))//扫描api的路径
                        .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                        .paths(PathSelectors.any())
                        .build();
          }
    
          @Bean
          public Docket createAdminApi() {
                return new Docket(DocumentationType.SWAGGER_2)
                      .groupName("后台管理服务接口文档")
                      .enable(!"prd".equals(active))
                      .apiInfo(apiInfo())
                      .select()
                      .apis(RequestHandlerSelectors.basePackage("com.controller.admin"))
                      .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                      .paths(PathSelectors.any())
                      .build();
           }
    
            private ApiInfo apiInfo() {
                  return new ApiInfoBuilder()
                      .title("网关服务接口文档")
                      .description("网关服务接口文档")
                      .version("1.0")
                      .build();
             }
    }
    

    4 关键点

    1、在SwaggerConfig 文件中一定要申明@EnableSwagger2不然启用不了
    2、在application启动文件不需要添加任何配置

    3、如果项目中使用到了RestControllerAdvice一定要注明仅在ResponseBody中生效例如全局异常处理: image.png
    4、如果项目中使用到了ResponseBodyAdvice需要将swagger设置为跳过: image.png

    5、如果访问swagger报错

    Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually:
    请按照第四部分设置好,特别是后面两个关键点如果有请一定要设置好

    相关文章

      网友评论

          本文标题:JAVA-springboot集成swagger

          本文链接:https://www.haomeiwen.com/subject/zmyrrdtx.html