swagger-ui -> 索引.html不存在 404 Spring Boot 3.1.5

swagger-ui -> index.html does not exists 404 spring boot 3.1.5

提问人:Sanady_ 提问时间:11/17/2023 最后编辑:Sanady_ 更新时间:11/17/2023 访问量:48

问:

我正在尝试将 Springdoc open-api 定义集成到我的项目中。目前由于某种奇怪的原因,/v3/api-docs 工作正常,它正在获取 API 的 JSON 版本,但 UI 不起作用,控制台正在打印找不到 index.html (404)。有没有办法使此功能起作用?有没有人在最新版本的 Springboot 中遇到类似的问题?UI

这是我的安全配置:

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

和 OpenAPI 初始化:


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

聚 甲醛:

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

安慰:

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
java spring-boot swagger-ui

评论

0赞 Andrei Lisa 11/17/2023
请确保您也已导入为依赖项org.springdoc:springdoc-openapi-starter-webmvc-ui
0赞 Sanady_ 11/17/2023
当我添加webmvc-ui时也是如此
0赞 Andrei Lisa 11/17/2023
从依赖项中删除io.swagger.core.v3
0赞 Sanady_ 11/17/2023
已删除,但结果相同。
0赞 Andrei Lisa 11/17/2023
你能访问路径吗?/swagger-ui/index.html

答:

0赞 g00glen00b 11/17/2023 #1

正如我在评论中提到的,由于您从 ,Spring Boot 将跳过 ,这反过来又导致 Swagger UI 自动配置也被跳过。WebMvcConfigurationSupportWebMvcAutoConfiguration

解决此问题的最简单方法是不要从 .如果确实需要提供额外的配置,可以从 进行扩展,但在大多数情况下,根本不需要对其进行扩展。WebmvcConfigurationSupportDelegatingWebMvcConfiguration