I use spring cloud and register my microservice in Consul. The microservice should work with JSON and XML accept types. Therefore the following 2 marshallers were added. However, when these 2 beans were implemented the consul health check start leads to the exception: HttpMediaTypeNotAcceptableException: Could not find acceptable representation
. If marshaller for XML is removed then the health check start works fine. Could you explain why adding marshaller for MediaType.APPLICATION_XML
leads to consul health check exception?
NB: I tried to send request /heath
to the application with headeraccept=application/json
through the curl and the answer was correct even when marshaller for MediaType.APPLICATION_XML
was enabled. Unfortunately, the consul sends the request with text/plain accept type.
@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;
}
and
@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;
}