Option for advanced ObjectMapper customization

Closes gh-23017
This commit is contained in:
Rossen Stoyanchev
2020-05-13 11:15:43 +01:00
parent e881d4b144
commit e88eb0ecf7
2 changed files with 36 additions and 0 deletions

View File

@@ -26,6 +26,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.function.Consumer;
import java.util.function.Function;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
@@ -169,6 +170,9 @@ public class Jackson2ObjectMapperBuilder {
@Nullable
private Boolean defaultUseWrapper;
@Nullable
private Consumer<ObjectMapper> configurer;
/**
* If set to {@code true}, an {@link XmlMapper} will be created using its
@@ -639,6 +643,19 @@ public class Jackson2ObjectMapperBuilder {
return this;
}
/**
* An option to apply additional customizations directly to the
* {@code ObjectMapper} instances at the end, after all other config
* properties of the builder have been applied.
* @param configurer a configurer to apply; if invoked multiple times, all
* configurers are applied in the same order.
* @since 5.3
*/
public Jackson2ObjectMapperBuilder postConfigurer(Consumer<ObjectMapper> configurer) {
this.configurer = (this.configurer != null ? this.configurer.andThen(configurer) : configurer);
return this;
}
/**
* Build a new {@link ObjectMapper} instance.
@@ -740,6 +757,10 @@ public class Jackson2ObjectMapperBuilder {
objectMapper.setHandlerInstantiator(
new SpringHandlerInstantiator(this.applicationContext.getAutowireCapableBeanFactory()));
}
if (this.configurer != null) {
this.configurer.accept(objectMapper);
}
}
private void registerModule(Module module, MultiValueMap<Object, Module> modulesToRegister) {