Search code examples
javaspringswaggerspringdoc

Adding Swagger (springdoc) to spring boot 3 | Java Spring boot


I want to add a swagger-ui to my spring application. I Followed some tutorials but nothing worked for me. Maybe somebody can give me some instructions how i can make it work. I would be very thankfull.

I got told that in spring 3 i should use

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

And it then should be accessable at http://server:port/context-path/swagger-ui.html In my case I configured the context path: server.servlet.context-path=/api/v1

but when i access it, i get the following error in the browser.

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).

In the Console i get the following output:

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

Solution

  • Try adding below line in application.properties

    springdoc.api-docs.enabled=true