Skip to content

Commit e6af48f

Browse files
committed
Polish "Retain existing modules in JacksonAutoConfiguration"
See gh-42836
1 parent 993fbb3 commit e6af48f

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

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

+12-6
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
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.Ordered;
7776
import org.springframework.core.annotation.Order;
7877
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
7978

@@ -327,12 +326,13 @@ void moduleBeansAndWellKnownModulesAreRegisteredWithTheObjectMapperBuilder() {
327326
}
328327

329328
@Test
330-
void customModulesRegisteredByBuilderCustomizerWithHighestPrecedenceShouldBeRetained() {
329+
void customModulesRegisteredByBuilderCustomizerShouldBeRetained() {
331330
this.contextRunner.withUserConfiguration(ModuleConfig.class, CustomModuleBuilderCustomizerConfig.class)
332331
.run((context) -> {
333332
ObjectMapper objectMapper = context.getBean(Jackson2ObjectMapperBuilder.class).build();
334333
assertThat(context.getBean(CustomModule.class).getOwners()).contains(objectMapper);
335-
assertThat(objectMapper.getRegisteredModuleIds()).contains("customizer-module");
334+
assertThat(objectMapper.getRegisteredModuleIds()).contains("module-A", "module-B",
335+
CustomModule.class.getName());
336336
});
337337
}
338338

@@ -608,9 +608,15 @@ Jackson2ObjectMapperBuilderCustomizer customDateFormat() {
608608
static class CustomModuleBuilderCustomizerConfig {
609609

610610
@Bean
611-
@Order(Ordered.HIGHEST_PRECEDENCE)
612-
Jackson2ObjectMapperBuilderCustomizer customModuleCustomizer() {
613-
return (builder) -> builder.modulesToInstall(new SimpleModule("customizer-module"));
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")));
614620
}
615621

616622
}

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

+2
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)