Spring Boot 3.x集成Swagger UI失败:无法访问Web界面求助
Spring Boot 3.x 中无法访问 Swagger UI 页面
我在Spring Boot 3.2.1版本中尝试运行Swagger UI,但无法通过地址 http://localhost:8080/webjars/swagger-ui/index.html#/ 访问Web界面。打开页面时显示资源找不到错误(提示无静态资源swagger-ui/index.html),且Docker容器无相关日志输出。
初始 build.gradle 配置
plugins { id 'java' id 'org.springframework.boot' version '3.2.1' id 'io.spring.dependency-management' version '1.1.4' } group = 'com.naman.jain' version = '0.0.1-SNAPSHOT' java { sourceCompatibility = '17' } configurations { compileOnly { extendsFrom annotationProcessor } } repositories { mavenCentral() } // Disables the default jar task, preventing the creation of a JAR file during the build process. // This is useful in scenarios where the project does not require a JAR file, // such as when only a WAR file is needed or in specific modules of a multi-module project. jar { enabled = false } dependencies { implementation project(':api') implementation project(':util') implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework.boot:spring-boot-starter-webflux' // https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-starter-webflux-ui // implementation group: 'org.springdoc', name: 'springdoc-openapi-starter-webflux-ui', version: '2.6.0' implementation 'org.springdoc:springdoc-openapi-starter-webflux-ui:2.5.0' // To avoid the following error message on Apple silicon (for details, see https://github.com/netty/netty/issues/11693): // Unable to load io.netty.resolver.dns.macos.MacOSDnsServerAddressStreamProvider, fallback to system defaults. This may result in incorrect DNS resolutions on MacOS. implementation group: 'io.netty', name: 'netty-resolver-dns-native-macos', classifier: 'osx-aarch_64' compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'io.projectreactor:reactor-test' } tasks.named('test') { useJUnitPlatform() }
服务容器启动日志
product-composite-1 | product-composite-1 | . ____ _ __ _ _ product-composite-1 | /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ product-composite-1 | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ product-composite-1 | \\/ ___)| |_)| | | | | || (_| | ) ) ) ) product-composite-1 | ' |____| .__|_| |_|_| |_\__, | / / / / product-composite-1 | =========|_|==============|___/=/_/_/_/ product-composite-1 | :: Spring Boot :: (v3.2.1) product-composite-1 | product-composite-1 | 2024-09-03T21:23:55.107Z INFO 1 --- [ main] j.c.p.ProductCompositeServiceApplication : Starting ProductCompositeServiceApplication v0.0.1-SNAPSHOT using Java 17.0.12 with PID 1 (/application/BOOT-INF/classes started by root in /application) product-composite-1 | 2024-09-03T21:23:55.124Z DEBUG 1 --- [ main] j.c.p.ProductCompositeServiceApplication : Running with Spring Boot v3.2.1, Spring v6.1.2 product-composite-1 | 2024-09-03T21:23:55.126Z INFO 1 --- [ main] j.c.p.ProductCompositeServiceApplication : The following 1 profile is active: "docker" product-composite-1 | 2024-09-03T21:24:01.708Z INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http) product-composite-1 | 2024-09-03T21:24:01.806Z INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] product-composite-1 | 2024-09-03T21:24:01.807Z INFO 1 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.17] product-composite-1 | 2024-09-03T21:24:02.169Z INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext product-composite-1 | 2024-09-03T21:24:02.185Z INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 6451 ms product-composite-1 | 2024-09-03T21:24:06.165Z INFO 1 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator' product-composite-1 | 2024-09-03T21:24:06.647Z INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '' product-composite-1 | 2024-09-03T21:24:06.705Z INFO 1 --- [ main] j.c.p.ProductCompositeServiceApplication : Started ProductCompositeServiceApplication in 13.851 seconds (process running for 15.837)
更新尝试:升级版本后问题仍未解决
已升级到Spring Boot 3.3.3和springdoc-openapi-starter-webflux-ui 2.6.0,但访问Swagger UI的问题依旧存在,更新后的build.gradle配置如下:
plugins { id 'java' id 'org.springframework.boot' version '3.3.3' id 'io.spring.dependency-management' version '1.1.6' } group = 'com.naman.jain' version = '0.0.1-SNAPSHOT' java { sourceCompatibility = '17' } configurations { compileOnly { extendsFrom annotationProcessor } } repositories { mavenCentral() } // Disables the default jar task, preventing the creation of a JAR file during the build process. // This is useful in scenarios where the project does not require a JAR file, // such as when only a WAR file is needed or in specific modules of a multi-module project. jar { enabled = false } dependencies { implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework.boot:spring-boot-starter-webflux' // https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-starter-webflux-ui implementation 'org.springdoc:springdoc-openapi-starter-webflux-ui:2.6.0' implementation project(':api') implementation project(':util') // To avoid the following error message on Apple silicon (for details, see https://github.com/netty/netty/issues/11693): // Unable to load io.netty.resolver.dns.macos.MacOSDnsServerAddressStreamProvider, fallback to system defaults. This may result in incorrect DNS resolutions on MacOS. implementation group: 'io.netty', name: 'netty-resolver-dns-native-macos', classifier: 'osx-aarch_64' compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'io.projectreactor:reactor-test' } tasks.named('test') { useJUnitPlatform() }
内容的提问来源于stack exchange,提问作者Naman Jain
相关产品推荐
相关产品推荐

