我使用SpringCloud并在Consul注册我的微服务。微服务应该使用JSON和XML接受类型。因此,增加了以下2名执法人员。但是,当这两个bean被实现时,consul健康检查启动会导致异常:
HttpMediaTypeNotAcceptableException: Could not find acceptable representation
. 如果删除了XML的封送拆收器,则运行状况检查开始工作正常。你能解释一下为什么要增加元帅吗?
MediaType.APPLICATION_XML
导致Consul健康检查异常?
NB:我试图发送请求
/heath
到带标题的应用程序
accept=application/json
通过旋涡和答案是正确的,即使当元帅
mediatype.application_xml
已启用。不幸的是,Consul使用文本/纯接受类型发送请求。
@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;
}