Swagger报错404

本文介绍了Swagger报错404的解决办法。作者按以往习惯用端口号加/swagger-ui.html路径访问Swagger出现404,后发现公司application.properties中有server.servlet.context-path=/shequapi配置,在访问路径上加/shequapi后访问成功。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Swagger报错404解决办法

我在进行开启Swager的时候,习惯性的使用了以前端口号+/swagger-ui.html的这一条路径(http://localhost:8088/shequapi/swagger-ui.html)

查看端口号

端口号
查看Swagger的访问地址
查看Swagger的访问地址
按照之前的配置访问Swagger出现了404
使用端口号+/swagger-ui.html404报错了

但是公司的application.properties中发现他有这个配置server.servlet.context-path=/shequapi

查看application.properties发现了server.servlet.context-path=/shequapi的配置查看application.properties后发现server.servlet.context-path=/shequapi
所以在访问swagger的时候地址不对
因此在访问路径上加上了/shequapi后就显示成功了
(http://localhost:8088/shequapi/swagger-ui.html)
加上/shequapi了后可以正常访问Swagger

### 解决 Spring Boot 项目中引入 Swagger 报错问题 在 Spring Boot 2.6.x 版本中,由于框架内部机制的变化以及依赖冲突等问题,在引入 `springfox-boot-starter` (即 Swagger 的实现) 后可能会遇到多种报错情况。以下是针对常见报错的解决方案。 #### 常见报错及其原因分析 1. **Failed to start bean ‘documentationPluginsBootstrapper’** 这种错误通常是因为 Spring Boot 2.6.x 默认禁用了 Bean 的循环引用检测功能[^1]。此外,Spring MVC 请求路径匹配器也发生了变化,从 `AntPathMatcher` 转变为 `PathPatternParser`,这可能导致某些旧版 Swagger 配置不兼容[^2]。 2. **NullPointerException 或其他运行时异常** 如果未正确配置 Swagger 或者存在版本不一致的情况,则可能引发 NullPointerException 等运行时异常[^3]。 --- #### 解决方案 ##### 方法一:调整 Maven 依赖并升级版本 确保使用的 `springfox-boot-starter` 是最新稳定版本,并与当前项目的 Spring Boot 版本保持兼容性。例如: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` 如果仍然存在问题,可以尝试切换到更高版本的 `springfox-boot-starter`,或者考虑使用 OpenAPI 工具替代 Swagger。 ##### 方法二:启用循环引用支持 对于因默认禁用循环引用而导致的问题,可以通过设置属性重新允许循环引用。修改 `application.properties` 文件如下: ```properties spring.main.allow-circular-references=true ``` 此操作可有效解决部分由循环引用引起的启动失败问题。 ##### 方法三:更改 Path Pattern Parser 行为 为了适配新的路径解析逻辑,可以在应用初始化阶段强制恢复至旧版行为 (`AntPathMatcher`)。通过自定义配置类完成这一目标: ```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) { configurer.setUseRegisteredSuffixPatternMatch(true); configurer.useSuffixPatternMatch(false); // 关闭后缀模式匹配 configurer.setPathMatcher(new AntPathMatcher()); // 使用传统方式 } } ``` 上述代码片段将路径匹配器重设为传统的 `AntPathMatcher` 实现。 ##### 方法四:排查 NPE 及其他潜在问题 当发生 NullPointer Exception 时,需重点检查以下几个方面: - 是否遗漏了必要的注解(如 `@EnableSwagger2` 或其等价形式); - API 文档扫描包范围是否覆盖实际接口所在的模块; - Docket 配置是否存在语法错误或参数缺失。 典型配置示例: ```java import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class SwaggerConfiguration { @Bean public Docket api() { return new Docket(DocumentationType.OAS_30) .apiInfo(apiDetails()) .select() .build(); } private ApiInfo apiDetails() { return new ApiInfoBuilder().title("My Application API").description("Documentation for My App") .version("1.0").build(); } } ``` 以上代码展示了如何创建一个基本的 Docket 对象实例。 --- ### 总结 综合来看,Spring Boot 2.6.x 中集成 Swagger 所产生的各类问题主要源于新特性带来的向后兼容性挑战。通过对依赖管理、核心配置文件调整以及必要扩展手段的应用,能够显著降低此类风险的发生概率。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值