Search code examples
javaspring-bootswagger-ui

Why am I getting 404 on swagger ui on Spring Boot 3.1.5?


I am trying to integrate the Springdoc open-api definition into my project. Currently for some strange reason /v3/api-docs is working properly, it is getting the JSON version of API but the UI does not work, console is printing that index.html is not found (404). Is there a way to make this feature work? Is there anyone facing similar problem with the newest version of the Springboot? UI

Here is my security configuration:

@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfiguration extends WebMvcConfigurationSupport {
    private final JwtAuthenticationFilter jwtAuthFilter;
    private final AuthenticationProvider authenticationProvider;

    private static final String[] URL_WHITELIST = {
            ApplicationConstants.AUTH + "/**",
            ApplicationConstants.PARKING_SPOT + ID,
            "/error",
            "/favicon.ico",
            "/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 securityFilterChain(HttpSecurity http) throws Exception {
        http
                .csrf(AbstractHttpConfigurer::disable)
                .authorizeHttpRequests(authorize -> authorize
                        .requestMatchers(URL_WHITELIST)
                        .permitAll()
                        .anyRequest()
                        .authenticated())
                .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
                .authenticationProvider(authenticationProvider)
                .addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class);
        return http.build();
    }
}

And OpenAPI initialization:


@Configuration
public class OpenApiConfiguration {
    @Bean
    public OpenAPI openAPI() {
        Info info = new Info()
                .title("ParkingTime API")
                .version("1.0");
        return new OpenAPI().info(info);
    }
}

Pom:

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

        <dependency>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>2.2.18</version>
        </dependency>

console:

2023-11-17T10:56:36.805+01:00  WARN 8272 --- [nio-8080-exec-1] o.s.web.servlet.PageNotFound             : No mapping for GET /swagger-ui/index.html
2023-11-17T10:56:37.440+01:00  WARN 8272 --- [nio-8080-exec-2] o.s.web.servlet.PageNotFound             : No mapping for GET /swagger-ui/index.html

Solution

  • As I mentioned in the comments, due to you extending from WebMvcConfigurationSupport, Spring Boot will skip the WebMvcAutoConfiguration, which in turn causes the Swagger UI autoconfiguration to be skipped as well.

    The easiest way to solve this is to not extend from WebmvcConfigurationSupport. If you really need to provide additional configuraton, you could extend from DelegatingWebMvcConfiguration, but in most cases, you don't need to extend it at all.