【发布时间】: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