配置介绍
| 环境 | 版本 |
|---|
| springboot | 2.7.0 |
| swagger | 2.9.2 |
Pom依赖
org.springframework.boot
spring-boot-starter-web
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
org.springframework.boot
spring-boot-starter-test
test
配置类
package com.timon.demo.config;
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.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration(proxyBeanMethods = false)
@EnableSwagger2
public class SwaggerConfig {
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/") // 需要映射到swagger文档中的路径
.select() // api选择构建器
// 需要配置到swagger文档中的接口
.apis(RequestHandlerSelectors.basePackage("com.timon.controller"))
.paths(PathSelectors.any()) // 所有接口都配合配置到swagger
.build().apiInfo(new ApiInfoBuilder()
.title("文档标题") // 当前文档标题
.description("文档描述")
.version("版本")
.contact(new Contact("Timon",
"https://blog.csdn.net/weixin_41934575?type=blog",
"543272045@qq.com"))
.build());
}
}
问题解决
ApplicationContextException: Failed to start bean ‘documentationPluginsBootstrapper’
在SpringBoot2.6.0以后的版本中将SpringMVC 默认路径匹配策略从AntPathMatcher 更改为PathPatternParser
【解决方案一】
application.properties文件中添加:
spring.mvc.pathmatch.matching-strategy = ant_path_matcher
指定使用ant_path_matcher进行路径匹配;
【解决方案二】
升级swagger版本