peligrosa
peligrosa

Reputation: 709

404 error on Swagger UI with Spring (springdoc-openapi configuration)

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

Upvotes: 16

Views: 51219

Answers (4)

Avidait Rojas
Avidait Rojas

Reputation: 1

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>

Upvotes: 0

Payam Soudachi
Payam Soudachi

Reputation: 489

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.

Upvotes: 0

naib khan
naib khan

Reputation: 1128

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();
    }

Upvotes: 14

peligrosa
peligrosa

Reputation: 709

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/

Upvotes: 43

Related Questions