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

SpringBoot3不支持旧版SpringDoc和Swagger。正确依赖是springdoc-openapi-starter-webmvc-ui。swagger-ui的正确访问路径是http://server:port/swagger-ui/index.html,而不是http://server:port/swagger-ui.html。Swagger的注解需要更新到Swagger3标准,例如将@ApiModelProperty替换为@Schema。MyBatis-Plus-Generator生成的代码需要调整@ApiModelProperty为@Schema,指定name和description属性。

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

本文针对 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;

其他待补充

出现这个错误的原因是在导入seaborn包时,无法从typing模块中导入名为&#39;Protocol&#39;的对象。 解决这个问题的方法有以下几种: 1. 检查你的Python版本是否符合seaborn包的要求,如果不符合,尝试更新Python版本。 2. 检查你的环境中是否安装了typing_extensions包,如果没有安装,可以使用以下命令安装:pip install typing_extensions。 3. 如果你使用的是Python 3.8版本以下的版本,你可以尝试使用typing_extensions包来代替typing模块来解决该问题。 4. 检查你的代码是否正确导入了seaborn包,并且没有其他导入错误。 5. 如果以上方法都无法解决问题,可以尝试在你的代码中使用其他的可替代包或者更新seaborn包的版本来解决该问题。 总结: 出现ImportError: cannot import name &#39;Protocol&#39; from &#39;typing&#39;错误的原因可能是由于Python版本不兼容、缺少typing_extensions包或者导入错误等原因造成的。可以根据具体情况尝试上述方法来解决该问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [ImportError: cannot import name ‘Literal‘ from ‘typing‘ (D:\Anaconda\envs\tensorflow\lib\typing....](https://blog.csdn.net/yuhaix/article/details/124528628)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

叼辣条闯天涯

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

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

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

打赏作者

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

抵扣说明:

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

余额充值