Spring Boot 3集成SpringDoc Swagger-UI遇404错误求助
Spring Boot 3集成Swagger-UI 404问题解决方法
依赖版本不兼容:你使用的
springdoc-openapi-ui:1.6.4是适配Spring Boot 2的版本,Spring Boot 3需要使用springdoc的2.x系列依赖,替换为starter包更适配:<dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>2.2.0</version> <!-- 可选用最新稳定2.x版本 --> </dependency>访问路径错误:Spring Boot 3搭配springdoc v2.x时,Swagger-UI的默认访问路径已变更。在配置
server.servlet.context-path=/api/v1后,正确的访问地址为:http://server:port/api/v1/swagger-ui/index.html
注意是swagger-ui/index.html,而非旧版的swagger-ui.html。静态资源放行配置(可选):如果上述调整后仍出现404,可添加Web配置类放行Swagger相关静态资源:
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/swagger-ui/**") .addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/"); } }也可通过配置文件指定Swagger-UI路径:
springdoc.swagger-ui.path=/swagger-ui/index.html
内容的提问来源于stack exchange,提问作者Leon
相关产品推荐
相关产品推荐

