Skip to content

How to configure MappingJackson2HttpMessageConverter registered by spring-hateoas? #262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ceefour opened this issue Oct 29, 2014 · 5 comments

Comments

@ceefour
Copy link

ceefour commented Oct 29, 2014

I'm sorry for misusing the issues as a forum, however there's already this question at StackOverflow http://stackoverflow.com/questions/24694493/how-to-configure-mappingjackson2httpmessageconverter-registered-by-spring-hateoa 3 months old and no solution.

@jcayetano
Copy link

Not an easy one, because it's a container of static inner classes. Easier to fork spring-hateoas project and apply your changes locally.

@dilipkrish
Copy link

@ceefour you should be able to access/ autowire the "_halObjectMapper" bean (don't remember for sure but I believe that's the qualifier name) in your spring configuration. Then the customizations are straight forward.

@ceefour
Copy link
Author

ceefour commented Dec 15, 2014

Thanks! Although that feels hacky or internals to me...

Actually the common use case would be to register additional modules such as JodaModule and friends... And also e.g. enable pretty-print JSON during development and disable it for production. Is there any "official" way to do it or planned?

@dschulten
Copy link
Contributor

In my experience, when you need to customize converters and modules it is also necessary to get their order right, and sooner or later you need full control. This is possible using WebMvcConfigurationSupport.configureMessageConverters (xml does not give you the same level of control, at least not in older versions of Spring). By letting your @configuration class extend WebMvcConfigurationSupport you effectively do the same as writing @EnableWebMvc, but you get more control.
You also need to replicate some internal setup logic of spring-hateoas, notably the rel provider plugin registry. Since it took me some time to figure that out, here is what I do:

@Configuration
@EnablePluginRegistries(RelProvider.class)
public class Config extends WebMvcConfigurationSupport {

private static final boolean EVO_PRESENT =
ClassUtils.isPresent("org.atteo.evo.inflector.English", null);

@Autowired
private PluginRegistry<RelProvider, Class<?>> relProviderRegistry;

@Override
protected void configureMessageConverters(List<HttpMessageConverter<?>>
converters) {
... converters that must come before jackson
converters.add(halConverter());
converters.add(jsonConverter());
... more converters
}

@Bean
public ObjectMapper jacksonObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(Include.NON_NULL);
return objectMapper;
}

@Bean
public MappingJackson2HttpMessageConverter jsonConverter() {
MappingJackson2HttpMessageConverter jacksonConverter = new
MappingJackson2HttpMessageConverter();
jacksonConverter.setSupportedMediaTypes(Arrays.asList(MediaType.valueOf("application/json")));
jacksonConverter.setObjectMapper(jacksonObjectMapper());
return jacksonConverter;
}

@Bean
public CurieProvider curieProvider() {
return new DefaultCurieProvider("your curie prefix", new UriTemplate("your curie base url template"));
}

@Bean
public MappingJackson2HttpMessageConverter halConverter() {
CurieProvider curieProvider = curieProvider();

RelProvider relProvider = new DelegatingRelProvider(relProviderRegistry);
ObjectMapper halObjectMapper = new ObjectMapper();

halObjectMapper.setSerializationInclusion(Include.NON_NULL);

halObjectMapper.registerModule(new Jackson2HalModule());
halObjectMapper.setHandlerInstantiator(new
Jackson2HalModule.HalHandlerInstantiator(relProvider, curieProvider));

MappingJackson2HttpMessageConverter halConverter = new
MappingJackson2HttpMessageConverter();
halConverter.setSupportedMediaTypes(Arrays.asList(MediaTypes.HAL_JSON));
halConverter.setObjectMapper(halObjectMapper);
return halConverter;
}

@Bean
RelProvider defaultRelProvider() {
return EVO_PRESENT ? new EvoInflectorRelProvider() : new DefaultRelProvider();
}

@Bean
RelProvider annotationRelProvider() {
return new AnnotationRelProvider();
}

}

@gregturn
Copy link
Contributor

gregturn commented Mar 5, 2019

Superceded by #833.

@gregturn gregturn closed this as completed Mar 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants