How to enable HAL rendering without using @EnableHypermediaSupport with spring hateoas?
hateoas, java, spring, spring-hateoas, spring-mvc
Solution
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 {
...
}
Problem
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: - Including a configuration class in existing mvc servlet xml configuration. - Using different versions of spring hateos (0.70.0, 0.80.0, 0.9.0.BUILD-SNAPSHOT), hoping that 0.90.0 will produce HAL out of the box, since, according to a jira issue, it should become the default rendering (at least for spring data rest). - Using a custom `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.