dynamic-extensions-for-alfresco icon indicating copy to clipboard operation
dynamic-extensions-for-alfresco copied to clipboard

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

Open titenkov opened this issue 4 years ago • 5 comments

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

titenkov avatar Jan 16 '21 11:01 titenkov

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.

kerkhofsd avatar Jan 16 '21 13:01 kerkhofsd

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.

titenkov avatar Jan 17 '21 10:01 titenkov

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 avatar Jan 18 '21 10:01 kerkhofsd

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

titenkov avatar Jan 19 '21 07:01 titenkov

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

tgeens avatar Jan 19 '21 14:01 tgeens