16

I am adding swagger UI to my Spring boot application. When I try to access the swagger-ui.html. I get the 404 error.

Config class :

@Configuration
public class SwaggerConfig {

    @Bean
    public OpenAPI springShopOpenAPI() {
        return new OpenAPI()
                .info(new Info().title("JOYAS-STOCK API Docs")
                        .description("JOYAS-STOCK REST API documentation")
                        .version("v1.0.0"));
    }
}

appliaction.properties :

#swagger-ui config
springdoc.swagger-ui.path=/swagger-ui
springdoc.swagger-ui.operationsSorter=method
springdoc.swagger-ui.tagsSorter=alpha

pom.xml :

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

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

There was an unexpected error (type=Not Found, status=404).

i started with the implementation of the configuration of swagger and apparently it's not working.

click to see screen of the error

0

4 Answers 4

43

Resolved.

the issue was in the versions, they were not compatible! i was using springdoc-openapi v1 with spring boot 3. which is wrong! with spring boot 3, springdoc-openapi v2 should be used. see documentation : https://springdoc.org/v2/

Sign up to request clarification or add additional context in comments.

2 Comments

LOL - that link gives a 404 as well - ditto swagger-ui/index.html :-)
In my case, the error was because I had springdoc-openapi-starter-common 2.2.0 and springdoc-openapi-starter-webmvc-ui 2.5.0. After putting both to 2.6.0, the error was gone.
14

With spring boot 3 you need to use springdoc-openapi v2.
For the integration between spring-boot and swagger-ui, add the library to the list of your project dependencies (No additional configuration is needed)

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

Note: This will automatically deploy swagger-ui to a spring-boot.

if you use spring security and want to access swagger UI (/swagger-ui/index.htm) without security check

private static final String[] AUTH_WHITELIST = {
            "/swagger-resources",
            "/swagger-resources/**",
            "/configuration/ui",
            "/configuration/security",
            "/swagger-ui.html",
            "/webjars/**",
            "/v3/api-docs/**",
            "/api/public/**",
            "/api/public/authenticate",
            "/actuator/*",
            "/swagger-ui/**"
    };
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests((authorize) -> authorize
                        .requestMatchers(AUTH_WHITELIST).permitAll()
                        .anyRequest().authenticated()
                )
                .httpBasic(withDefaults());
        return http.build();
    }

1 Comment

in case there is a proxy on k8s, it ll add a path prefix, the swagger ui html will load, the config will throw 404
0

I encountered an issue related to loading the Swagger configuration file. If you inspect the browser's console, you may see an error message similar to this:

Failed to load resource: the server responded with a status of 403 ()
spec-actions.js:19 undefined /api-docs/swagger-config
i @ spec-actions.js:19

The problem was that the configuration was defined in the application.yml file as follows:

springdoc:
  api-docs:
    path: /api-docs
  swagger-ui:
    path: /swagger-ui.html

To resolve the issue, it was necessary to permit the /api-docs/** URLs.

Comments

0

I added the dependency that appears in the following link https://springdoc.org/ and the error was solved.

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

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.