In my Spring Boot application, I have assigned the HAL object-mapper to MappingJackson2HttpMessageConverter
. This is because my custom media-types end with +json
and end up being recognized by the default converter. The custom MappingJackson2HttpMessageConverter
instance registered by Spring HATEOAS only recognizes application/hal+json
. My custom media-types are of the form application/vnd.service.entity.v1.hal+json
, which are recognized by the default instance (because it supports application/json
and application/*+json
). However, the default instance does not serialize links properly into the HAL convention. I have been able to get around it by registering the mapper like so:
@Bean
public HttpMessageConverters customConverters() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(Arrays.asList(
new MediaType("application", "json", Charset.defaultCharset()),
new MediaType("application", "*+json", Charset.defaultCharset()),
new MediaType("application", "hal+json")
));
ObjectMapper halObjectMapper = beanFactory.getBean(HAL_OBJECT_MAPPER_BEAN_NAME, ObjectMapper.class);
converter.setObjectMapper(halObjectMapper);
return new HttpMessageConverters(converter);
}
There is a concern that I am now polluting regular JSON serialization/deserialization with HAL concerns, but I cannot think of any other way (short of explicitly specifying every single custom media-type I use) to do this. Thoughts?