Reputation: 3829
I am trying to add a new type to the jackson converter, its a custom type but its really just json and the standard object mapper should be ok to convert it to java objects.
I am trying the following:
@Bean
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter()
{
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
MediaType fhir = new MediaType("application", "json+fhir");
List<MediaType> jacksonTypes = new ArrayList<>(converter.getSupportedMediaTypes());
jacksonTypes.add(fhir);
converter.setSupportedMediaTypes(jacksonTypes);
return converter;
}
When I debug the application on start up and look at the list of supported types I can see the fhir media type in there but I still get the same error:
Could not extract response: no suitable HttpMessageConverter found for response type [class MyClass] and content type [application/json+fhir]
I am looking for some help to get the message converter to work with this custom type.
Thanks
** Edit - I fixed this using Stanislavs answer below:
@Bean
RestTemplate restTemplate()
{
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
messageConverters.add(mappingJackson2HttpMessageConverter());
return restTemplate;
}
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter()
{
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
converter.setObjectMapper(objectMapper);
MediaType fhir = new MediaType("application", "json+fhir");
List<MediaType> jacksonTypes = new ArrayList<>(converter.getSupportedMediaTypes());
jacksonTypes.add(fhir);
converter.setSupportedMediaTypes(jacksonTypes);
return converter;
}
Upvotes: 2
Views: 2388
Reputation: 57381
Guess your converter is not registered properly. I think Jackson separately add the converter and your bean is just ignored.
See the example
Just create your converter class extending MappingJackson2HttpMessageConverter
and register it like this
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
<mvc:message-converters register-defaults="true">
<bean class="com.example.MyMappingJackson2HttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
Upvotes: 2