user2023141
user2023141

Reputation: 1223

Spring springdoc-openapi : swagger-ui/index.html cannot be found status=404

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

Upvotes: 3

Views: 33124

Answers (6)

simo
simo

Reputation: 373

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.

Upvotes: 0

Matthieu Saleta
Matthieu Saleta

Reputation: 1518

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.

Upvotes: 9

Vikas s kumar
Vikas s kumar

Reputation: 335

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>

Upvotes: 1

hamidreza75
hamidreza75

Reputation: 707

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

Upvotes: 3

user2023141
user2023141

Reputation: 1223

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}"

Upvotes: 9

Grim
Grim

Reputation: 2030

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

Upvotes: 4

Related Questions