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版本变化,路径匹配策略有了改动
比任何人都要努力
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 我与微信审核的“相爱相杀”看个人小程序副业
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~