【问题标题】:Spring boot 1.5 consul health check 406 error (HttpMediaTypeNotAcceptableException)spring boot 1.5 consul健康检查406错误(HttpMediaTypeNotAcceptableException)
【发布时间】:2018-05-29 14:30:28
【问题描述】:

我使用 spring cloud 并在 Consul 中注册了我的微服务。微服务应该使用 JSON 和 XML 接受类型。因此添加了以下 2 个编组器。但是,当实施这 2 个 bean 时,领事健康检查开始会导致异常:HttpMediaTypeNotAcceptableException: Could not find acceptable representation。如果删除了 XML 编组器,则运行状况检查开始工作正常。你能解释一下为什么为 MediaType.APPLICATION_XML 添加 marshaller 会导致 consul 健康检查异常吗?

注意:我尝试通过 curl 使用 headeraccept=application/json 向应用程序发送请求 /heath,即使启用了 MediaType.APPLICATION_XML 的编组器,答案也是正确的。不幸的是,领事发送了带有 text/plain 接受类型的请求。

    @Bean
public MarshallingHttpMessageConverter marshallingHttpMessageConverter() {
    Jaxb2Marshaller jaxb2Marshaller = jaxb2Marshaller();

    MarshallingHttpMessageConverter marshallingHttpMessageConverter = new MarshallingHttpMessageConverter();
    marshallingHttpMessageConverter.setMarshaller(jaxb2Marshaller);
    marshallingHttpMessageConverter.setUnmarshaller(jaxb2Marshaller);
    List<MediaType> supportedMediaTypes = new ArrayList<>();
    supportedMediaTypes.add(MediaType.APPLICATION_XML);
    marshallingHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
    return marshallingHttpMessageConverter;
}

和

    @Bean
public MappingJackson2HttpMessageConverter marshallingHttpMessageConverterJson() {

    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setObjectMapper(new ObjectMapper());
    List<MediaType> supportedMediaTypes = new ArrayList<>();
    supportedMediaTypes.add(MediaType.APPLICATION_JSON);
    converter.setSupportedMediaTypes(supportedMediaTypes);
    return converter;
}

【问题讨论】:

    标签: java spring-boot spring-cloud-consul


    【解决方案1】:

    我将我的应用程序从 Spring Boot 1.4.1 迁移到 1.5.9,上面提到的所有 bean 都是在 1.4.1 中实现的。但是,以下配置中也提到了这些 bean:

    @Configuration
    @EnableWebMvc
    public class WebConfig extends WebMvcConfigurerAdapter {
    
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.clear();
    
        converters.add(marshallingHttpMessageConverter());
        converters.add(marshallingHttpMessageConverterJson());
    }
    

    当这个被覆盖的方法被移除后,一切都开始正常工作了。

    【讨论】:

      猜你喜欢
      • 2019-08-05
      • 2017-05-27
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 2020-03-15
      • 2016-01-27
      • 1970-01-01
      相关资源
      最近更新 更多