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

springdoc-openapi-ui访问返回406 Not Acceptable错误求助

解决springdoc-openapi-ui访问出现406错误的问题

问题描述

使用Spring Boot 2.7.7集成springdoc-openapi-ui 1.6.14,访问http://localhost:8080/swagger-ui/index.html时出现406错误(服务器无法生成请求接受的媒体类型)。相关配置如下:

pom.xml依赖

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

Spring Security配置

@Configuration
@EnableWebSecurity
public class SpringSecurityConfig {

  @Bean
  public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/").permitAll();
        http.headers().frameOptions().sameOrigin();
        return http.build();
  }
  
  @Bean
  public WebSecurityCustomizer webSecurityCustomizer() {
      return web -> web.ignoring().antMatchers("/swagger-ui/**", "/v3/api-docs/**");
  }
}

可能原因及解决方案

1. 自定义消息转换器导致JSON媒体类型支持缺失

如果项目中自定义了WebMvcConfigurer并重写了configureMessageConverters方法,可能意外移除了默认的JSON消息转换器,导致服务器无法返回application/json类型的响应(swagger-ui请求依赖该类型)。

解决方法:确保配置中保留JSON转换器:

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 添加JSON消息转换器
        converters.add(new MappingJackson2HttpMessageConverter(new ObjectMapper()));
        // 其他自定义转换器按需添加
    }
}

2. springdoc版本兼容性或基础配置缺失

尝试升级到适配Spring Boot 2.7.x的最新springdoc版本,同时配置基础的OpenAPI Bean确保文档生成正常:

升级依赖版本:

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

添加OpenAPI配置类:

@Configuration
public class SpringDocConfig {
    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info().title("API文档").version("v1"));
    }
}

3. 浏览器缓存或请求头异常

浏览器缓存的旧请求头可能导致Accept类型不匹配,尝试用无痕模式访问,或清理浏览器缓存后重试。

4. 依赖冲突检查

确认项目中存在jackson相关依赖(Spring Boot默认包含,若被排除需手动添加):

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 08:56:09