【SpringDoc】SpringBoot 3 swagger-ui.html 报404 SpringDoc 官方文档问题(可能)

本文针对 SpringBoot 3.x 版本!!

SpringDoc v2 官方文档:https://springdoc.org/v2/

首先

旧版本的 swagger 和 1.x 版本的SpringDoc 均不支持 SpringBoot 3,所以对于 SpringBoot3 如下的两种 Swagger 或 SpringDoc 依赖均不可用,注意,是不可用

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>x.x.x</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>x.x.x</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>x.x.x</version>
</dependency>

正确的 SpringDoc 依赖

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.1.0</version>
</dependency>

其次(最坑人的地方)

无数的文章甚至官方文档都表示,swagger-ui 界面的网址是 http://server:port/swagger-ui.html,访问这个网址,一直是404,天王老子来了也是404,就算在 application.properties 中设置了如下内容,也不管用

springdoc.swagger-ui.path=/swagger-ui.html

但是打开 Demo Spring Boot 3 Web MVC with OpenAPI 3 这个官方提供的 Demo 后,显示的网址是 http://server:port/swagger-ui/index.html, 把网址换成这个,瞬间打开!!

所以,正确的解决办法就是,访问 http://server:port/swagger-ui/index.html

Swagger (SpringFox) 迁移到 SpringDoc 后注解写法补充

官方文档中仅给出了如下说明:

Replace swagger 2 annotations with swagger 3 annotations (it is already included with springdoc-openapi-starter-webmvc-ui dependency). Package for swagger 3 annotations is io.swagger.v3.oas.annotations.
@Api → @Tag
@ApiIgnore → @Parameter(hidden = true) or @Operation(hidden = true) or @Hidden
@ApiImplicitParam → @Parameter
@ApiImplicitParams → @Parameters
@ApiModel → @Schema
@ApiModelProperty(hidden = true) → @Schema(accessMode = READ_ONLY)
@ApiModelProperty → @Schema
@ApiOperation(value = “foo”, notes = “bar”) → @Operation(summary = “foo”, description = “bar”)
@ApiParam → @Parameter
@ApiResponse(code = 404, message = “foo”) → @ApiResponse(responseCode = “404”, description = “foo”)

但是 MyBatis-Plus-Generator 生成的代码,在 entity 属性上的注解方法是

@ApiModelProperty("密码")
private String password;

文档只告诉我,要把 @ApiModelProperty 改成 @Schema 改完就报错啊,汗。正确的改法如下:

@Schema(name = "password", description = "密码")
private String password;

其他待补充

### 解决Spring Boot 3Swagger UI访问404错误的方法 在Spring Boot 3项目中遇到Swagger UI返回404 Not Found的问题通常是因为路径配置不正确或是依赖项版本兼容性问题。为了确保能够正常访问Swagger文档页面,需确认以下几个方面: #### 1. 添加正确的依赖库 对于Spring Boot 3.x版本来说,推荐使用`springdoc-openapi-starter-webmvc-ui`来替代旧版的swagger-springfox组合[^2]。 ```xml <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>2.0.2</version><!-- 版本号应根据实际情况调整 --> </dependency> ``` #### 2. 配置文件设置 确保application.yml或properties中有如下配置以启用API文档功能并指定UI界面地址[^4]: ```yaml spring: mvc: pathmatch: matching-strategy: ant_path_matcher # 如果存在WebFlux模块则需要此配置 webflux: base-path: /api/v1/ # 可选配置,默认为空字符串"" springdoc: api-docs: path: /v3/api-docs swagger-ui: path: /swagger-ui.html ``` #### 3. 启动类扫描包范围设定 如果应用结构较为复杂,则可能需要显式指明组件扫描的基础包位置以及排除不必要的自动装配选项[^1]。 ```java @SpringBootApplication(scanBasePackages = "com.example", exclude={SolrAutoConfiguration.class}) public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 完成上述修改之后重启应用程序,在浏览器输入`http://localhost:<port>/swagger-ui.html`即可查看到Swagger接口列表页;其中<port>代表实际使用的端口号,比如默认情况下就是8080。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叼辣条闯天涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值