Skip to content

Commit 751afe2

Browse files
committed
Merge pull request #42836 from nosan
* pr/42836: Polish "Retain existing modules in JacksonAutoConfiguration" Retain existing modules in JacksonAutoConfiguration Closes gh-42836
2 parents ff6c7c7 + e6af48f commit 751afe2

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ private Field findPropertyNamingStrategyField(String fieldName) {
307307
}
308308

309309
private void configureModules(Jackson2ObjectMapperBuilder builder) {
310-
builder.modulesToInstall(this.modules.toArray(new Module[0]));
310+
builder.modulesToInstall((modules) -> modules.addAll(this.modules));
311311
}
312312

313313
private void configureLocale(Jackson2ObjectMapperBuilder builder) {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import org.springframework.context.annotation.Configuration;
7474
import org.springframework.context.annotation.Import;
7575
import org.springframework.context.annotation.Primary;
76+
import org.springframework.core.annotation.Order;
7677
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
7778

7879
import static org.assertj.core.api.Assertions.assertThat;
@@ -324,6 +325,17 @@ void moduleBeansAndWellKnownModulesAreRegisteredWithTheObjectMapperBuilder() {
324325
});
325326
}
326327

328+
@Test
329+
void customModulesRegisteredByBuilderCustomizerShouldBeRetained() {
330+
this.contextRunner.withUserConfiguration(ModuleConfig.class, CustomModuleBuilderCustomizerConfig.class)
331+
.run((context) -> {
332+
ObjectMapper objectMapper = context.getBean(Jackson2ObjectMapperBuilder.class).build();
333+
assertThat(context.getBean(CustomModule.class).getOwners()).contains(objectMapper);
334+
assertThat(objectMapper.getRegisteredModuleIds()).contains("module-A", "module-B",
335+
CustomModule.class.getName());
336+
});
337+
}
338+
327339
@Test
328340
void defaultSerializationInclusion() {
329341
this.contextRunner.run((context) -> {
@@ -592,6 +604,23 @@ Jackson2ObjectMapperBuilderCustomizer customDateFormat() {
592604

593605
}
594606

607+
@Configuration(proxyBeanMethods = false)
608+
static class CustomModuleBuilderCustomizerConfig {
609+
610+
@Bean
611+
@Order(-1)
612+
Jackson2ObjectMapperBuilderCustomizer highPrecedenceCustomizer() {
613+
return (builder) -> builder.modulesToInstall((modules) -> modules.add(new SimpleModule("module-A")));
614+
}
615+
616+
@Bean
617+
@Order(1)
618+
Jackson2ObjectMapperBuilderCustomizer lowPrecedenceCustomizer() {
619+
return (builder) -> builder.modulesToInstall((modules) -> modules.add(new SimpleModule("module-B")));
620+
}
621+
622+
}
623+
595624
@Configuration(proxyBeanMethods = false)
596625
static class ObjectMapperBuilderConsumerConfig {
597626

spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/spring-mvc.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ Such customizer beans can be ordered (Boot's own customizer has an order of 0),
118118
Any beans of type javadoc:com.fasterxml.jackson.databind.Module[] are automatically registered with the auto-configured javadoc:org.springframework.http.converter.json.Jackson2ObjectMapperBuilder[] and are applied to any javadoc:com.fasterxml.jackson.databind.ObjectMapper[] instances that it creates.
119119
This provides a global mechanism for contributing custom modules when you add new features to your application.
120120

121+
NOTE: If you wish to register additional modules programmatically using a javadoc:org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer[], make sure to use the `modulesToInstall` method that takes a consumer as the other variants are not additive.
122+
121123
If you want to replace the default javadoc:com.fasterxml.jackson.databind.ObjectMapper[] completely, either define a javadoc:org.springframework.context.annotation.Bean[format=annotation] of that type or, if you prefer the builder-based approach, define a javadoc:org.springframework.http.converter.json.Jackson2ObjectMapperBuilder[] javadoc:org.springframework.context.annotation.Bean[format=annotation].
122124
When defining an javadoc:com.fasterxml.jackson.databind.ObjectMapper[] bean, marking it as javadoc:org.springframework.context.annotation.Primary[format=annotation] is recommended as the auto-configuration's javadoc:com.fasterxml.jackson.databind.ObjectMapper[] that it will replace is javadoc:org.springframework.context.annotation.Primary[format=annotation].
123125
Note that, in either case, doing so disables all auto-configuration of the javadoc:com.fasterxml.jackson.databind.ObjectMapper[].

0 commit comments

Comments
 (0)