You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Springfox UI文档访问抛出HTTP 405错误,请求解决方案

解决Springfox Swagger UI访问405错误(GET请求不支持)的问题

针对你遇到的访问swagger-ui.html报405错误,但v2/api-docs能正常访问的问题,结合你使用的Springfox 2.8.0版本,我整理了几个针对性的解决方案,你可以逐一尝试:

1. 确保Swagger静态资源被正确映射

Spring Boot可能没有自动识别Swagger UI的静态资源路径,导致swagger-ui.html无法被正确解析。你可以添加一个WebMvc配置类,手动配置资源映射:

@Configuration
public class SwaggerResourceConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 映射swagger-ui.html到classpath下的META-INF/resources
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        // 映射webjars资源
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

2. 明确允许Swagger路径的GET请求

虽然你的Security配置已经放开了Swagger相关路径,但可能没有明确指定允许GET方法。修改configure(HttpSecurity http)方法,明确指定HTTP方法:

import org.springframework.http.HttpMethod;

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.cors().and().csrf().disable().authorizeRequests()
            .antMatchers("/no-auth/**").permitAll()
            // 明确允许GET请求访问所有Swagger相关路径
            .antMatchers(HttpMethod.GET, 
                         "/v2/api-docs", 
                         "/swagger-resources/configuration/ui", 
                         "/swagger-resources", 
                         "/swagger-resources/configuration/security", 
                         "/swagger-ui.html", 
                         "/webjars/**").permitAll()
            .anyRequest().authenticated()
            .and()
            .addFilter(new JWTAuthenticationFilter(securityConfiguration, authenticationManager()))
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}

3. 排除JWT过滤器对Swagger路径的拦截

你添加的JWTAuthenticationFilter可能会拦截Swagger相关的请求,导致请求被提前处理而返回错误。在过滤器中添加路径排除逻辑:

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
    String requestUri = request.getRequestURI();
    
    // 排除所有Swagger相关路径,直接放行
    if (requestUri.startsWith("/v2/api-docs") ||
        requestUri.startsWith("/swagger-resources") ||
        requestUri.equals("/swagger-ui.html") ||
        requestUri.startsWith("/webjars")) {
        chain.doFilter(request, response);
        return;
    }
    
    // 原有JWT验证逻辑
    // ...
}

4. 确认依赖完整性

检查你的pom.xml(或build.gradle)是否正确引入了springfox-swagger-ui依赖,这是Swagger UI页面的核心依赖:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.8.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.8.0</version>
</dependency>

完成以上配置后,重启应用,再访问http://localhost:8080/swagger-ui.html应该就能正常加载了。

内容的提问来源于stack exchange,提问作者conquester

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:32:53