javaspring-bootspring-cloud-consul

Spring boot 1.5 consul health check 406 error (HttpMediaTypeNotAcceptableException)


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;
}

Solution

  • I migrated my application from spring boot 1.4.1 to 1.5.9 and all mentioned above bean where implemented in 1.4.1. However, these beans were mentioned also in the following configuration:

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

    When this overridden method was removed, everything starts work fine.