Spring Boot 3.0+Springdoc OpenAPI配置自定义上下文路径及Swagger UI
解决Spring Boot 3 + Springdoc OpenAPI路径配置问题
1. 配置应用全局上下文路径
直接在application.properties(或application.yml)里设置应用的默认上下文:
server.servlet.context-path=/myapp
2. 修正Springdoc路径配置
你之前的配置重复添加了/myapp前缀,会导致访问路径变成/myapp/myapp/xxx,属于无效配置。调整为以下内容:
# OpenAPI JSON文档访问路径:/myapp/api-docs springdoc.api-docs.path=/api-docs # Swagger UI访问路径:/myapp/swagger-ui.html springdoc.swagger-ui.path=/swagger-ui.html # 自定义YAML文档需带上上下文前缀,确保资源能被正确访问 springdoc.swagger-ui.urls.url=/myapp/test-service.yaml
配置验证
启动应用后,通过以下地址确认是否生效:
- Swagger UI:
http://localhost:你的端口/myapp/swagger-ui.html - OpenAPI JSON文档:
http://localhost:你的端口/myapp/api-docs
内容的提问来源于stack exchange,提问作者Prabagaran Sellamuthu
相关产品推荐
相关产品推荐

