Spring Boot 2.6.x整合Swagger启动失败报错问题解决

问题

Spring Boot 2.6.x版本引入依赖 springfox-boot-starter (Swagger 3.0) 后,启动容器会报错:

Failed to start bean ‘ documentationPluginsBootstrapper ‘ ; nested exception…

原因
Springfox 假设 Spring MVC 的路径匹配策略是 ant-path-matcher,而 Spring Boot 2.6.x版本的默认匹配策略是 path-pattern-matcher,这就造成了上面的报错。

解决方案
方案一(治标)
在 application.properties 配置文件中修改mvc的匹配策略

spring.mvc.pathmatch.matching-strategy=ant-path-matcher

注意:开始的时候我用这个方法的确可以正常启动了,但后来我发现此方法在某些服务启动时会失效!我查了一下才发现这个方法治标不治本,具体如下:

只有在不使用 Spring Boot 的执行器时,此功能才起作用。
无论配置的匹配策略如何,执行器将始终使用基于路径模式的解析 ( 也就是默认策略 ) 。
如果您想在 Spring Boot 2.6及更高版本中将其与执行器一起使用,则需要对 Sp

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!要将Spring Boot 2.6Swagger 3.0整合在一起,你可以按照以下步骤进行操作: 步骤1:添加Swagger依赖 在你的Spring Boot项目的pom.xml文件中,添加Swagger的依赖: ```xml <dependencies> <!-- 其他依赖 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> </dependencies> ``` 步骤2:配置Swagger 创建一个Swagger配置类,用于配置Swagger的相关信息: ```java import org.springframework.context.annotation.Configuration; import springfox.documentation.swagger2.annotations.EnableSwagger2; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; @Configuration @EnableSwagger2 public class SwaggerConfig { public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("API 文档") .description("API 文档") .version("1.0.0") .build(); } } ``` 步骤3:启用Swagger 在你的Spring Boot应用程序的主类上使用`@EnableSwagger2`注解来启用Swagger: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Import; @SpringBootApplication @Import(SwaggerConfig.class) public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 步骤4:访问Swagger UI 在启动你的应用程序后,你可以通过访问`http://localhost:8080/swagger-ui/`来查看生成的API文档。 以上就是将Spring Boot 2.6Swagger 3.0整合的基本步骤。你可以根据自己的需要进一步定制和配置Swagger。希望能对你有所帮助!如果有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值