3

I've a spring boot application (2.5.6) with a dependency on springdoc-openapi. However, launching swagger-ui (http://localhost:8080/v1/swagger-ui/index.html) doesn't work. The debug logs are indicating that index.html is not present. What could be the reason that index.html is not found ?

enter image description here

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

application.yaml

springdoc:
  swagger-ui:
    tagsSorter: alpha
    operations-sorter: alpha
    doc-expansion: none
    disable-swagger-default-url: true

logging:
  level:
    root: DEBUG

server:
  port: 8080

spring:
  application:
    name: fe-applic-app
    api-version: "v1"

enter image description here

6 Answers 6

10

I found the cause of the problem. The context-path was not set in application.yaml.

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

After adding servlet : context-path, swagger-ui is rendered

server:
  port: 8080
  servlet:
    context-path: "/${spring.application.api-version}"
Sign up to request clarification or add additional context in comments.

1 Comment

awesome, great answer, fixed my problem. Based on this I added something similar in my application.properties spring.application.api-version=v1 server.servlet.context-path=/${spring.application.api-version}
9

To share my experience because I have just spend more than 1 day to resolve a similar problem.

This can happen if the project contains a configuration class extending WebMvcConfigurationSupport.

To be more precise :

  • The /v3/api-docs route was working
  • The /swagger-ui/index.html was throwing a 404 error

As I said, we had inside the project a configuration class extending WebMvcConfigurationSupport in order to automatically convert lowercase String inside the request to an uppercase Enumeration (How to make enum parameter lowercase in SpringBoot/Swagger?)

In this case, Spring creates first a bean extending WebMvcConfigurationSupport where the addResourceHandlers(ResourceHandlerRegistry) method does nothing (Empty method).

When this configuration class is not present, Spring creates a bean DelegatingWebMvcConfiguration extending WebMvcConfigurationSupport but this class overrides the addResourceHandlers(ResourceHandlerRegistry) method to register all the WebMvcConfigurer beans to the ResourceHandlerRegistry.

So it adds the SwaggerWebMvcConfigurer that manages the route swagger-ui/index.html.

2 Comments

This was the cause of the problem for me, thank you! I solved it by refactoring the class that extended WebMvcConfigurationSupport to implement WebMvcConfigurer instead.
Thank you very much, this answer saved many hours of my life :)
4

Try swagger-ui.html instead of swagger-ui/index.html. In 60% of the cases swagger-ui.html redirects to its real location.

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

Comments

3

add this to your configs:

@Configuration
public class WebAppConfig extends WebMvcConfigurationSupport
{

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        registry.addResourceHandler("/swagger-ui/**")
        .addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.14.3/");
    }

see source and static-resources

Comments

1

If you are using open API with spring 3. It will give 404 error. Hence use the springdoc-openapi-starter-webmvc-ui dependency. Later you can use the URL's

  • http://localhost:8080/v3/api-docs

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

Eg

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

Comments

0

Even though I'm dealing with a newer version of springboot I had exactly the same issue with a specific version of SpringBoot

  • Spring boot 3.2.10 (with springdoc 2.6.0) was working, showing the swagger page
  • Spring boot 3.4.2 (with springdoc 2.7.x or 2.8.x) wasn't showing the page

I was blaming the newer version of springboot, but there was probably something wrong with my maven cache. I delete all the org.spring* stuff and this fixed the issue. If this didn't work, I would also suggest cleaning all the IDE caches.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.