-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Support adding rather than replacing modules in Jackson2ObjectMapperBuilder #28633
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
Comments
@sbrannen hi,Sam. Does this issue need to be achieved? I personally think it would be more flexible to provide a method to add a Module separately, if it need to be implemented, can I be assigned? I would be happy to provide PR. |
@aooohan, we will discuss within the team whether we wish to add such a method. Please note that the |
Alright, thank you for your reply. |
We could maybe add a |
Please keep it simple. We are already in a |
Note that the builder currently has two methods for modules, An overloaded method with a |
Please note that you always need two methods (unless you refactor the whole thing to avoid the Semantically, I wish to just add a module to the builder, so I would appreciate it, if there is a method for exactly that (similar to all the other existing methods). I explicitly added the two code examples in the issue description to show the difference in their implementation. Most of the other methods look like the serializer one (are adders not setters). This is the workaround I currently use: @Bean
// Custom error details
public Jackson2ObjectMapperBuilderCustomizer apiErrorCustomizer() {
// We cannot use the module directly
// because the module methods are only setters and not adders
// builder#serializers adds the passed serializers instead
return builder -> builder.serializers(ApiErrorModule.requiredSerializers())
.deserializers(ApiErrorModule.requiredDeserializers());
}
// --------------------- in another configuration class/artifact
@Bean
// "2000" -> "2000-01-01" <= x < "2001-01-01"
public Jackson2ObjectMapperBuilderCustomizer dateishRangeCustomizer() {
return builder -> builder.serializers(DatishRangeModule.requiredSerializers())
.deserializers(DatishRangeModule.requiredDeserializers());
} |
We could consider making |
Team Decision: we've decided to go with the @Bean
public Jackson2ObjectMapperBuilderCustomizer customizer() {
return builder -> builder.modules(list -> list.add(...));
} As a result, the methods can be added immediately in the 5.3.x branch. |
spring-framework/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java
Lines 570 to 574 in 67b1420
Currently the
Jackson2ObjectMapperBuilderCustomizer
is only able to replace the modules that should be installed; however, it is possible to define multiple Spring BootJackson2ObjectMapperBuilderCustomizer
beans that could each wish to add a Jackson module. However the later customizers will always remove the modules configured by the previous instance. It isn't possible to get the current list of modules either and extend that.We wish to separate our customizers by their related modules/source libraries (api-docs, error-handling, ..., and the application itself).
So it would be nice if there was a
addModules(toInstall)
method that would add the modules to the list instead of replacing it.Most of the other methods such as
serializers(JsonSerializer<?>...)
already work like that:spring-framework/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java
Lines 354 to 363 in 67b1420
Thanks for your awesome work.
The text was updated successfully, but these errors were encountered: