在 Spring Boot 3 中使用 Openapi 无法打开 swagger-ui(Whitelabel 错误页面)

问题描述 投票:0回答:1

我在 Spring Boot 中通过 Openapi 打开 swagger-ui 时遇到问题。

当我尝试打开此 URL http://localhost:8080/swagger-ui.html 时,我收到 Whitelabel 错误页面

我该如何解决这个问题?

这是在 pom.xml 中定义的 dependency,如下所示。

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.6.4</version>
</dependency>

这是如下所示的 openpi 配置类

@Configuration
public class OpenApiConfig {

    @Bean
    public OpenAPI customOpenAPI(@Value("${application-description}") String description,
                                 @Value("${application-version}") String version) {
        return new OpenAPI()
                .info(new Info().title("API")
                        .version(version)
                        .description(description)
                        .license(new License().name("API Licence")));
    }
}

这是如下所示的 application.properties 文件。

springdoc.swagger-ui.path=/swagger-ui.html
application-description=API Description
application-version=1.0

logging.level.org.springframework.web=DEBUG
logging.level.io.springfox=DEBUG

当我尝试打开此网址时,出现如下错误

http://localhost:8080/swagger-ui.html

2023-02-09T08:36:16.593+03:00 DEBUG 20184 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : GET "/swagger-ui.html", parameters={}
2023-02-09T08:36:16.594+03:00 DEBUG 20184 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2023-02-09T08:36:16.596+03:00 DEBUG 20184 --- [nio-8080-exec-4] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2023-02-09T08:36:16.596+03:00 DEBUG 20184 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND
2023-02-09T08:36:16.597+03:00 DEBUG 20184 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
2023-02-09T08:36:16.597+03:00 DEBUG 20184 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2023-02-09T08:36:16.599+03:00 DEBUG 20184 --- [nio-8080-exec-4] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2023-02-09T08:36:16.599+03:00 DEBUG 20184 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 404

我该如何解决这个问题?

这是回购协议:链接

java spring-boot swagger-ui openapi springdoc-openapi-ui
1个回答
5
投票

您需要为 Spring Boot 3 使用不同的依赖项:

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

此处查找当前版本。

© www.soinside.com 2019 - 2024. All rights reserved.