gregwhitaker / catnap

Partial JSON response framework for RESTful web services
Apache License 2.0
55 stars 15 forks source link

How to inject custom object mapper with Spring boot ? #10

Closed scizeron closed 7 years ago

scizeron commented 7 years ago

Hi,

I would like to leverage all jackson object mapper features which are integrated with spring boot in using Catnap but I realized that CatnapJsonMessageConverter owns its own objectMapper.

For example, when I define this : SerializationFeature.WRITE_DATES_AS_TIMESTAMPS If I remove Catnap, my dates are like this : 2014-03-27T23:00:00+0000 but if I use Catnap, I have a timestamp value.

For the moment, in spring-boot, I made a little hack. I collect the objectMapper from the MappingJackson2HttpMessageConverter and I remove the existing catnapJsonMessageConverter and I add a new one like this : converters.add(new CatnapJsonMessageConverter(new JsonCatnapView.Builder().withObjectMapper(jsonMapper).build()));

Is it possible to have the same behaviour, with Catanap and with Jackson without doing things like this ?

Tell me that yo think about this?

gregwhitaker commented 7 years ago

What you did was how you would make that work today when using the @EnableCatnap annotation. I agree that it makes for a frustrating user experience.

Perhaps what we could do when processing that annotation is to check for an ObjectMapper bean in the spring context and use that instead of the default object mapper in catnap. What do you think about that?

scizeron commented 7 years ago

Hi,

Thanks for your reply. Yes it's an enhancement and the main idea is this one : try to get the ObjectMapper bean provided by spring and use it in order to have the same behavior (with or without Catnap). For the moment, here is my workaround :

Stéphane

MWiels commented 7 years ago

Hi @gregwhitaker , Do you have any updates on the proposed solution to use the objectmapper provided by Spring? Matthias

gregwhitaker commented 7 years ago

Catnap will now auto-detect the ObjectMapper instance defined in the spring configuration and automatically configure it to be used by Catnap. If you do not supply an object mapper a default one will be created for Catnap. Please refer to the springboot example project to see this in action.

This functionality has been added to catnap 2.2.0, available on jcenter.

scizeron commented 7 years ago

Hi, Sorry for the delay, I was on vacation. Thank you for taking time to consider my request. It is almost what I expected. In fact, if we follow the spring-boot guidelines about how to customize an objectMapper, you can use the default Jackson2ObjectMapperBuilder or your own bean. From this, you can generate ObjectMapper instances with customisations. Otherwise you can also define your own ObjectMapper bean, marked as Primary. In your improvement, if I don't publish my own custom ObjectMapper, I will not leverage your improvement and I don't leverage spring-boot customizations (via yaml config file).

In your configuration code, you can keep your non required ObjectMapper. If it exist, you can use it otherwise you wire the Jackson2ObjectMapperBuilder instance (whatever if it's the default one or a custom replacing it), it always exists and you can inject an ObjectMapper from it to ensure the same behaviour in terms of JSON features.

Stéphane

gregwhitaker commented 7 years ago

@scizeron Sounds good. Thanks for the pull request. I'll go ahead and make a new release with your changes.

scizeron commented 7 years ago

Thanks for your great library.