Spring Boot整合Swagger2.9.2版本出现的小问题

访问接口文档地址时,单个应用能正常访问swagger-ui.html页面,但F12看到请求接口有报错:
在这里插入图片描述
但因为在gateway网关进行了各个服务的接口文档聚合,所以在gateway访问时一直报错,因此最后只能降版本,讲到Swagger2.8.0版本。
然后请求时相比2.9.2版本少了两个请求:
在这里插入图片描述

这算是2.9.x版本的一个小问题吧,可以看github提交的问题

### 解决方案 Spring Boot 整合 Swagger 2.9.2 后无法正常访问的问题通常由多种原因引起,以下是详细的分析和解决方案。 #### 1. **路径匹配机制冲突** 从提供的资料来看,问题的核心在于 Spring Boot 2.6.x 版本引入了 `PathPatternParser` 路径匹配器作为默认实现,而 Swagger 基于的是传统的 `AntPathMatcher`[^3]。这种差异可能导致某些路径解析失败,从而引发 404 错误。 ##### 修改配置以兼容旧版路径匹配逻辑 可以通过调整 Spring Boot 的路径匹配策略来解决此问题: ```java import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void configurePathMatch(PathMatchConfigurer configurer) { // 设置为 AntPathMatcher,默认是 PathPatternParser configurer.setUseRegisteredSuffixPatternMatch(true); configurer.setUseTrailingSlashMatch(true).setPathMatcher(new org.springframework.util.AntPathMatcher()); } } ``` 通过上述代码片段,可以强制 Spring Boot 使用 `AntPathMatcher` 来处理请求路径匹配,从而避免与 Swagger 的路径定义发生冲突。 --- #### 2. **依赖版本一致性** 确保 Maven 或 Gradle 中的依赖项正确无误,并且各组件之间的版本相互兼容。以下是一个典型的 Spring BootSwagger 集成所需的依赖列表: ```xml <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> ``` 如果使用其他工具链(如 OpenAPI),则需额外验证其是否支持当前使用的 Spring Boot 版本[^1]。 --- #### 3. **启用Swagger功能** 确认已正确启用了 Swagger 功能,在主类或者配置文件中添加如下注解: ```java import springfox.documentation.swagger2.annotations.EnableSwagger2; @SpringBootApplication @EnableSwagger2 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 同时还需要创建一个 Bean 定义 Docket 对象以便自定义 API 文档的行为: ```java import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration public class SwaggerConfiguration { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("My RESTful APIs").description("").version("1.0").build(); } } ``` 以上设置能够帮助开发者更灵活地控制哪些接口会被纳入到文档生成范围之内[^2]。 --- #### 4. **检查静态资源映射规则** 有时即使完成了上述操作仍然会遇到页面加载失败的情况,这可能是因为前端 UI 文件未被正确暴露给外部客户端访问所致。此时可以在应用启动时打印日志查看实际部署位置并手动指定资源目录地址: ```properties spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ ``` 将该属性加入 application.properties 或者 yaml 格式的配置文件即可解决问题。 --- ### 总结 综上所述,针对 Spring Boot 整合 Swagger 2.9.2 后无法正常访问的现象可以从以下几个方面入手排查:一是修改全局路径匹配算法;二是核对第三方库间的协作关系;三是开启必要的扫描开关;四是重新规划公共资源定位方式。每一步都至关重要,缺一不可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值