springboot2.6.4的两种集成swagger2的方式.md

前景:我在做伙伴匹配系统时,后端需要集成swagger框架测试接口,因为我的springboot是springboot2,所以用swagger2,但是在集成过程中,遇到了一个bug:NullPointerException,原因是版本问题

  • 解决NullPointerException异常:(在yml配置文件加入以下配置)
spring: 
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

好了,接下来完整的介绍两种集成swagger2的方式,仅供springboot2参考

第一种:原生swagger2

  • 引入依赖
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>
  • yml配置
spring: 
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
  • swaggerConfig类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 这里一定是自己的位置
                .apis(RequestHandlerSelectors.basePackage("com.guo.mate.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("用户中心 API")
                .description("接口文档swagger2")
                .termsOfServiceUrl("http://localhost:8080/")
                .contact(new Contact("guo","https://guo.cn/","3201786134@qq.com"))
                .version("1.0")
                .build();
    }
}
  • 访问接口文档http://localhost:8080/swagger-ui.html

第二种: Knife4j

[!TIP]

建议使用Knife4j,对swagger2的增强;

并且可以通过在 controller 方法上添加 @Api、 @ApiOperation(value = "向客人问好") 等注解来自定义生成的接口描述信息。

千万注意:线上环境不要把接口暴露出去!!!可以通过在 SwaggerConfig 配置文件开头加上 @Profile({"dev", "test"}) 限定配置仅在部分环境开启

  • 引入依赖
<!-- knife4j 接口文档 -->
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>2.0.7</version>
</dependency>
  • yml配置
spring:   
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
  profiles:
    active: dev
  • swaggerConfig类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

@Configuration
@EnableSwagger2WebMvc
@Profile({"dev", "test"})   //版本控制访问
public class Swagger2Config {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 这里一定是自己的位置
                .apis(RequestHandlerSelectors.basePackage("com.guo.mate.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("用户中心 API")
                .description("接口文档swagger2")
                .termsOfServiceUrl("http://localhost:8080/")
                .contact(new Contact("guo","https://guo.cn/","3201786134@qq.com"))
                .version("1.0")
                .build();
    }
}
  • 访问接口文档http://localhost:8080/api/doc.html

[!IMPORTANT]

总结:

1.遇到bug不要害怕,一步一步解决

2.EnableSwagger2WebMvc和EnableSwagger2注解好像是一样的功能

3.上线时,api接口文档不能暴露

4.springboot版本变化,路径匹配策略有了改动

posted @ 2024-08-14 11:22  gdxstart  阅读(153)  评论(0)    收藏  举报