Skip to content
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

Customize ObjectMapper used for @RequestBody and @ResponseBody (de)serialization #318

Open
titenkov opened this issue Jan 16, 2021 · 5 comments
Labels

Comments

@titenkov
Copy link

I'm submitting a ... (check one with "x")

[ ] bug report
[ ] feature request
[x] question

Expected Behavior

Is it possible to override the global MappingJackson2HttpMessageConverter / ObjectMapper, which is used by default? I would like to modify it a bit (date formats, snake case, etc.). I was trying to do that in the "spring way", but seems like it doesn't work. I've noticed the annotation @MessageConverter, but there is no documentation of how to use that.

Could you please suggest an approach for it?

Context

I'm investigating if we can use dynamic extensions for some Alfresco customization (custom REST API). Currently, we're using https://github.com/dgradecak/alfresco-mvc for this purpose.

Your Environment

  • Alfresco version used: 5.2.7.2
  • DE version used: 6.0.5
@kerkhofsd
Copy link
Contributor

Hi @titenkov ,

I'm afraid customizing the MappingJackson2HttpMessageConverter that is used for the @RequestBody and @ResponseBody functionality is currently not possible.

As an alternative for the @RequestBody functionality, you could create a custom ArgumentResolver in which you use your custom ObjectMapper. (See second part of this section in the documentation.)
Here you can find an example of such an ArgumentResolver implementation.
Once you've provided such an ArgumentResolver, Dynamic-Extensions will automatically convert matching arguments of methods annotated with the @Uri annotation.

I had a look at the MessageConverter annotation provided in the project. It indeed looks like it was introduced to support custom message converters. However at this point it's not used / it has no implementation.

Possible improvements and feedback are always welcome! Being able to customize the MappingJackson2HttpMessageConverter / ObjectMapper sounds very useful to me. However if we would make this possible on the level of the Dynamic-Extensions framework, it would have (possibly unwanted) impact on all bundles that are build upon framework.

@titenkov
Copy link
Author

Thank you for a quick reply @kerkhofsd :)

I will try out the ArgumentResolver approach instead of @RequestBody.
What about @ResponseBody? Is there any alternative to it?

In general, it would be very useful to have the ability to customize an ObjectMapper and it's strange that I'm the first who is asking about that. I'm happy to contribute to the project if you have an idea of how that should be implemented.

@kerkhofsd
Copy link
Contributor

Hi @titenkov ,

Unfortunately there is no good alternative for @ResponseBody at this point. You could always write to the output stream directly, but we definitely are not going to call that a good alternative.

FYI: the MessageConverterRegistry is the class currently responsible for initializing and registering custom MessageConverters:
https://github.com/xenit-eu/dynamic-extensions-for-alfresco/blob/79467d172129efb3b410cf4c46f40dc26a2076b9/annotations-runtime/src/main/java/com/github/dynamicextensionsalfresco/webscripts/MessageConverterRegistry.java
An important observation here is that a new MessageConverterRegistry is initialized for each bundle. Hence customizing this in the bundle context doesn't affect other bundles / extensions.

After a discussion with the team, we see two options:

  1. AS-IS, it is actually possible to modify the ObjectMapper used for (de)serialization.
    This would require 1) adding a dependency on the annotations-runtime jar and 2) injecting the MessageConverterRegistry bean in your code. Thereafter you are able to retrieve the MappingJackson2HttpMessageConverter from the registry and customize as preferred.
    This is just a "hacky" workaround to achieve the requirement.

  2. We want and should provide a good approach to make JSON de(serialization) customization possible.
    After a first look it would make sense to provide an equivalent of Spring's Jackson2ObjectMapperBuilderCustomizer, used to customize the ObjectMapper if an instance is available in the Spring context.
    You indicated you were trying to do this "the Spring way": is this what you had in mind?

We would love to discuss and receive any contributions to add this nice improvement to the framework!

@kerkhofsd kerkhofsd changed the title Is it possible to override the MappingJackson2HttpMessageConverter / ObjectMapper, which are used by default? Customize ObjectMapper used for @RequestBody and @ResponseBody (de)serialization Jan 18, 2021
@titenkov
Copy link
Author

Yeah, supporting Jackson2ObjectMapperBuilderCustomizer would be a good option, I think.

@tgeens
Copy link
Member

tgeens commented Jan 19, 2021

@titenkov Is there anything in particular that makes you look for alternatives to https://github.com/dgradecak/alfresco-mvc ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants