【问题标题】:Adding Swagger (springdoc) to spring boot 3 | Java Spring boot将 Swagger (springdoc) 添加到 spring boot 3 | Java 春季启动
【发布时间】:2023-02-16 23:19:55
【问题描述】:

我想在我的 spring 应用程序中添加一个 swagger-ui。我遵循了一些教程,但对我没有任何帮助。也许有人可以给我一些说明,告诉我如何让它工作。我会非常感激。

我被告知在 spring 3 我应该使用

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

然后它应该可以访问 http://server:port/context-path/swagger-ui.html 在我的例子中,我配置了上下文路径: server.servlet.context-path=/api/v1

但是当我访问它时,我在浏览器中收到以下错误。

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Nov 30 18:53:23 CET 2022
There was an unexpected error (type=Not Found, status=404).

在控制台中,我得到以下输出:

No active profile set, falling back to 1 default profile: "default"
Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
Finished Spring Data repository scanning in 44 ms. Found 5 MongoDB repository interfaces.
Tomcat initialized with port(s): 8080 (http)
Starting service [Tomcat]
Starting Servlet engine: [Apache Tomcat/10.1.1]
Initializing Spring embedded WebApplicationContext
Root WebApplicationContext: initialization completed in 728 ms
ControllerAdvice beans: 0 @ModelAttribute, 0 @InitBinder, 1 RequestBodyAdvice, 1 ResponseBodyAdvice
18 mappings in 'requestMappingHandlerMapping'
Patterns [/webjars/**, /**] in 'resourceHandlerMapping'
ControllerAdvice beans: 1 @ExceptionHandler, 1 ResponseBodyAdvice
Tomcat started on port(s): 8080 (http) with context path '/api/v1'
Started BackendApplication in 1.737 seconds (process running for 2.133)
Initializing Spring DispatcherServlet 'dispatcherServlet'
Initializing Servlet 'dispatcherServlet'
Detected StandardServletMultipartResolver
Detected AcceptHeaderLocaleResolver
Detected FixedThemeResolver
Detected org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@66aec079
Detected org.springframework.web.servlet.support.SessionFlashMapManager@29895b06
enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
Completed initialization in 1 ms
GET "/api/v1/swagger-ui.html", parameters={}
Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
Resource not found
Completed 404 NOT_FOUND
"ERROR" dispatch for GET "/api/v1/error", parameters={}
Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
Selected 'text/html' given [text/html, text/html;q=0.8]
Exiting from "ERROR" dispatch, status 404

【问题讨论】:

  • 尝试访问 /swagger-ui/index.html 而不是 /swagger-ui.html
  • @ILyaCyclone 感谢您的回答,遗憾的是它不起作用。我犯了同样的错误。
  • 我建议首先让它在没有 /context-path 的情况下工作,因为可能需要额外的配置和错误的地方。

标签: java spring swagger springdoc


【解决方案1】:

尝试在 application.properties 中添加以下行

springdoc.api-docs.enabled=true

【讨论】:

    猜你喜欢
    • 2021-12-30
    • 2020-05-03
    • 1970-01-01
    • 2018-06-26
    • 2021-06-01
    • 2020-12-18
    • 2019-07-21
    • 1970-01-01
    • 2017-09-11
    相关资源
    最近更新 更多