With @EnableHypermediaSupport(type = HypermediaType.HAL)
Spring hateoas provides a simple and convenient way to enable HAL rendering.
The annotations triggers some configuration magic which is explained here: https://github.com/spring-projects/spring-hateoas#enablehypermediasupport
However if you are working on a given xml-config based spring application, it is not easy to integrate @EnableHypermediaSupport. I tried a lot of different ways to enable HAL rendering but no single solution was working correctly:
MappingJackson2HttpMessageConverter
to register a new Jackson2HalModule()
also did not work. Although the converter was used for rendering (for example to render Dates), the output was not rendered in HAL.Therefore my question: How can I enable HAL rendering without using EnableHypermediaSupport?
I intentionally left out any code snippets, because I don't think that it would help much.
Based on @WaldemarSchneider answer, here is a concrete how-to:
Create an HTTP message converter:
public class HalHttpMessageConverter extends AbstractJackson2HttpMessageConverter {
public HalHttpMessageConverter() {
super(new ObjectMapper(), new MediaType("application", "hal+json", DEFAULT_CHARSET));
objectMapper.registerModule(new Jackson2HalModule());
objectMapper.setHandlerInstantiator(new Jackson2HalModule.HalHandlerInstantiator(new DefaultRelProvider(), null));
// customize your mapper if needed
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
}
@Override
protected boolean supports(Class<?> clazz) {
return ResourceSupport.class.isAssignableFrom(clazz);
}
}
Register it in your servlet context:
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="package.to.HalHttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
And don't forget to make your rest controllers produce application/hal+json
:
@RestController
@RequestMapping(value = "/articles", produces = "application/hal+json")
public class ArticleRestController {
...
}