Spring boot 1.5 consul健康检查406错误(HttpMediaTypeNotAcceptableException)

Spring boot 1.5 consul health check 406 error (HttpMediaTypeNotAcceptableException)

我使用 spring 云并在 Consul 中注册我的微服务。微服务应使用 JSON 和 XML 接受类型。因此添加了以下 2 个编组器。然而,当这 2 个 bean 被实施时,consul 健康检查开始导致异常: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;
}

我将我的应用程序从 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());
}

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