Skip to content

Commit e88eb0e

Browse files
committed
Option for advanced ObjectMapper customization
Closes gh-23017
1 parent e881d4b commit e88eb0e

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Locale;
2727
import java.util.Map;
2828
import java.util.TimeZone;
29+
import java.util.function.Consumer;
2930
import java.util.function.Function;
3031

3132
import com.fasterxml.jackson.annotation.JsonAutoDetect;
@@ -169,6 +170,9 @@ public class Jackson2ObjectMapperBuilder {
169170
@Nullable
170171
private Boolean defaultUseWrapper;
171172

173+
@Nullable
174+
private Consumer<ObjectMapper> configurer;
175+
172176

173177
/**
174178
* If set to {@code true}, an {@link XmlMapper} will be created using its
@@ -639,6 +643,19 @@ public Jackson2ObjectMapperBuilder applicationContext(ApplicationContext applica
639643
return this;
640644
}
641645

646+
/**
647+
* An option to apply additional customizations directly to the
648+
* {@code ObjectMapper} instances at the end, after all other config
649+
* properties of the builder have been applied.
650+
* @param configurer a configurer to apply; if invoked multiple times, all
651+
* configurers are applied in the same order.
652+
* @since 5.3
653+
*/
654+
public Jackson2ObjectMapperBuilder postConfigurer(Consumer<ObjectMapper> configurer) {
655+
this.configurer = (this.configurer != null ? this.configurer.andThen(configurer) : configurer);
656+
return this;
657+
}
658+
642659

643660
/**
644661
* Build a new {@link ObjectMapper} instance.
@@ -740,6 +757,10 @@ else if (this.applicationContext != null) {
740757
objectMapper.setHandlerInstantiator(
741758
new SpringHandlerInstantiator(this.applicationContext.getAutowireCapableBeanFactory()));
742759
}
760+
761+
if (this.configurer != null) {
762+
this.configurer.accept(objectMapper);
763+
}
743764
}
744765

745766
private void registerModule(Module module, MultiValueMap<Object, Module> modulesToRegister) {

spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilderTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import com.fasterxml.jackson.databind.deser.Deserializers;
6363
import com.fasterxml.jackson.databind.deser.std.DateDeserializers;
6464
import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair;
65+
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
6566
import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector;
6667
import com.fasterxml.jackson.databind.module.SimpleModule;
6768
import com.fasterxml.jackson.databind.module.SimpleSerializers;
@@ -431,6 +432,20 @@ public void filters() throws JsonProcessingException {
431432
assertThat(output).doesNotContain("value2");
432433
}
433434

435+
@Test // gh-23017
436+
public void postConfigurer() {
437+
438+
JacksonAnnotationIntrospector introspector1 = new JacksonAnnotationIntrospector();
439+
JacksonAnnotationIntrospector introspector2 = new JacksonAnnotationIntrospector();
440+
441+
ObjectMapper mapper = Jackson2ObjectMapperBuilder.json()
442+
.postConfigurer(m -> m.setAnnotationIntrospectors(introspector1, introspector2))
443+
.build();
444+
445+
assertThat(mapper.getSerializationConfig().getAnnotationIntrospector()).isSameAs(introspector1);
446+
assertThat(mapper.getDeserializationConfig().getAnnotationIntrospector()).isSameAs(introspector2);
447+
}
448+
434449
@Test
435450
public void completeSetup() throws JsonMappingException {
436451
NopAnnotationIntrospector introspector = NopAnnotationIntrospector.instance;

0 commit comments

Comments
 (0)