Apply standard Jackson2ObjectMapperBuilder config via a customizer
This commit builds on the new abstraction introduced in 2a0b8a7 by applying the standard JacksonProperties-based Jackson2ObjectMapperBuilder configuration via a customizer. It also applies some polishing to the original contribution: - Code has been formatted - Logic that applies the customizers has moved to be alongside the code that creates the builder - Logic that explicitly sorted the customisers has been removed as they will be sorted automatically prior to injection
This commit is contained in:
parent
deb7529a36
commit
0c2ecb7ba0
|
@ -17,6 +17,7 @@
|
||||||
package org.springframework.boot.autoconfigure.jackson;
|
package org.springframework.boot.autoconfigure.jackson;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -20,8 +20,8 @@ import java.lang.reflect.Field;
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Locale;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
@ -42,7 +42,6 @@ import org.joda.time.format.DateTimeFormat;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.beans.factory.BeanFactoryUtils;
|
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||||
import org.springframework.beans.factory.ListableBeanFactory;
|
import org.springframework.beans.factory.ListableBeanFactory;
|
||||||
import org.springframework.beans.factory.ObjectProvider;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.JavaVersion;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.JavaVersion;
|
||||||
|
@ -53,7 +52,7 @@ import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Primary;
|
import org.springframework.context.annotation.Primary;
|
||||||
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
|
@ -88,30 +87,13 @@ public class JacksonAutoConfiguration {
|
||||||
@ConditionalOnClass({ ObjectMapper.class, Jackson2ObjectMapperBuilder.class })
|
@ConditionalOnClass({ ObjectMapper.class, Jackson2ObjectMapperBuilder.class })
|
||||||
static class JacksonObjectMapperConfiguration {
|
static class JacksonObjectMapperConfiguration {
|
||||||
|
|
||||||
private final List<Jackson2ObjectMapperBuilderCustomizer> builderCustomizers;
|
|
||||||
|
|
||||||
JacksonObjectMapperConfiguration(
|
|
||||||
ObjectProvider<List<Jackson2ObjectMapperBuilderCustomizer>> builderCustomizersProvider) {
|
|
||||||
this.builderCustomizers = builderCustomizersProvider.getIfAvailable();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Primary
|
@Primary
|
||||||
@ConditionalOnMissingBean(ObjectMapper.class)
|
@ConditionalOnMissingBean(ObjectMapper.class)
|
||||||
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
|
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
|
||||||
customize(builder);
|
|
||||||
return builder.createXmlMapper(false).build();
|
return builder.createXmlMapper(false).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void customize(Jackson2ObjectMapperBuilder builder) {
|
|
||||||
if (this.builderCustomizers != null) {
|
|
||||||
AnnotationAwareOrderComparator.sort(this.builderCustomizers);
|
|
||||||
for (Jackson2ObjectMapperBuilderCustomizer customizer : this.builderCustomizers) {
|
|
||||||
customizer.customize(builder);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@ -180,24 +162,70 @@ public class JacksonAutoConfiguration {
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnClass({ ObjectMapper.class, Jackson2ObjectMapperBuilder.class })
|
@ConditionalOnClass({ ObjectMapper.class, Jackson2ObjectMapperBuilder.class })
|
||||||
@EnableConfigurationProperties(JacksonProperties.class)
|
|
||||||
static class JacksonObjectMapperBuilderConfiguration {
|
static class JacksonObjectMapperBuilderConfiguration {
|
||||||
|
|
||||||
private final ApplicationContext applicationContext;
|
private final ApplicationContext applicationContext;
|
||||||
|
|
||||||
|
JacksonObjectMapperBuilderConfiguration(ApplicationContext applicationContext,
|
||||||
|
JacksonProperties jacksonProperties,
|
||||||
|
List<Jackson2ObjectMapperBuilderCustomizer> customizers) {
|
||||||
|
this.applicationContext = applicationContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean(Jackson2ObjectMapperBuilder.class)
|
||||||
|
public Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder(
|
||||||
|
List<Jackson2ObjectMapperBuilderCustomizer> customizers) {
|
||||||
|
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
|
||||||
|
builder.applicationContext(this.applicationContext);
|
||||||
|
customize(builder, customizers);
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void customize(Jackson2ObjectMapperBuilder builder,
|
||||||
|
List<Jackson2ObjectMapperBuilderCustomizer> customizers) {
|
||||||
|
for (Jackson2ObjectMapperBuilderCustomizer customizer : customizers) {
|
||||||
|
customizer.customize(builder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnClass({ ObjectMapper.class, Jackson2ObjectMapperBuilder.class })
|
||||||
|
@EnableConfigurationProperties(JacksonProperties.class)
|
||||||
|
static class Jackson2ObjectMapperBuilderCustomizerConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public StandardJackson2ObjectMapperBuilderCustomizer standardJacksonObjectMapperBuilderCustomizer(
|
||||||
|
ApplicationContext applicationContext,
|
||||||
|
JacksonProperties jacksonProperties) {
|
||||||
|
return new StandardJackson2ObjectMapperBuilderCustomizer(applicationContext,
|
||||||
|
jacksonProperties);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class StandardJackson2ObjectMapperBuilderCustomizer
|
||||||
|
implements Jackson2ObjectMapperBuilderCustomizer, Ordered {
|
||||||
|
|
||||||
|
private final ApplicationContext applicationContext;
|
||||||
|
|
||||||
private final JacksonProperties jacksonProperties;
|
private final JacksonProperties jacksonProperties;
|
||||||
|
|
||||||
JacksonObjectMapperBuilderConfiguration(ApplicationContext applicationContext,
|
StandardJackson2ObjectMapperBuilderCustomizer(
|
||||||
|
ApplicationContext applicationContext,
|
||||||
JacksonProperties jacksonProperties) {
|
JacksonProperties jacksonProperties) {
|
||||||
this.applicationContext = applicationContext;
|
this.applicationContext = applicationContext;
|
||||||
this.jacksonProperties = jacksonProperties;
|
this.jacksonProperties = jacksonProperties;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Override
|
||||||
@ConditionalOnMissingBean(Jackson2ObjectMapperBuilder.class)
|
public int getOrder() {
|
||||||
public Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder() {
|
return 0;
|
||||||
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
|
}
|
||||||
builder.applicationContext(this.applicationContext);
|
|
||||||
|
@Override
|
||||||
|
public void customize(Jackson2ObjectMapperBuilder builder) {
|
||||||
|
|
||||||
if (this.jacksonProperties.getDefaultPropertyInclusion() != null) {
|
if (this.jacksonProperties.getDefaultPropertyInclusion() != null) {
|
||||||
builder.serializationInclusion(
|
builder.serializationInclusion(
|
||||||
this.jacksonProperties.getDefaultPropertyInclusion());
|
this.jacksonProperties.getDefaultPropertyInclusion());
|
||||||
|
@ -214,7 +242,6 @@ public class JacksonAutoConfiguration {
|
||||||
configurePropertyNamingStrategy(builder);
|
configurePropertyNamingStrategy(builder);
|
||||||
configureModules(builder);
|
configureModules(builder);
|
||||||
configureLocale(builder);
|
configureLocale(builder);
|
||||||
return builder;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void configureFeatures(Jackson2ObjectMapperBuilder builder,
|
private void configureFeatures(Jackson2ObjectMapperBuilder builder,
|
||||||
|
@ -240,9 +267,11 @@ public class JacksonAutoConfiguration {
|
||||||
(DateFormat) BeanUtils.instantiateClass(dateFormatClass));
|
(DateFormat) BeanUtils.instantiateClass(dateFormatClass));
|
||||||
}
|
}
|
||||||
catch (ClassNotFoundException ex) {
|
catch (ClassNotFoundException ex) {
|
||||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
|
||||||
// Since Jackson 2.6.3 we always need to set a TimeZone (see gh-4170)
|
dateFormat);
|
||||||
// If none in our properties fallback to the Jackson's default
|
// Since Jackson 2.6.3 we always need to set a TimeZone (see
|
||||||
|
// gh-4170). If none in our properties fallback to the Jackson's
|
||||||
|
// default
|
||||||
TimeZone timeZone = this.jacksonProperties.getTimeZone();
|
TimeZone timeZone = this.jacksonProperties.getTimeZone();
|
||||||
if (timeZone == null) {
|
if (timeZone == null) {
|
||||||
timeZone = new ObjectMapper().getSerializationConfig()
|
timeZone = new ObjectMapper().getSerializationConfig()
|
||||||
|
@ -258,7 +287,8 @@ public class JacksonAutoConfiguration {
|
||||||
Jackson2ObjectMapperBuilder builder) {
|
Jackson2ObjectMapperBuilder builder) {
|
||||||
// We support a fully qualified class name extending Jackson's
|
// We support a fully qualified class name extending Jackson's
|
||||||
// PropertyNamingStrategy or a string value corresponding to the constant
|
// PropertyNamingStrategy or a string value corresponding to the constant
|
||||||
// names in PropertyNamingStrategy which hold default provided implementations
|
// names in PropertyNamingStrategy which hold default provided
|
||||||
|
// implementations
|
||||||
String strategy = this.jacksonProperties.getPropertyNamingStrategy();
|
String strategy = this.jacksonProperties.getPropertyNamingStrategy();
|
||||||
if (strategy != null) {
|
if (strategy != null) {
|
||||||
try {
|
try {
|
||||||
|
@ -287,7 +317,8 @@ public class JacksonAutoConfiguration {
|
||||||
Assert.notNull(field, "Constant named '" + fieldName + "' not found on "
|
Assert.notNull(field, "Constant named '" + fieldName + "' not found on "
|
||||||
+ PropertyNamingStrategy.class.getName());
|
+ PropertyNamingStrategy.class.getName());
|
||||||
try {
|
try {
|
||||||
builder.propertyNamingStrategy((PropertyNamingStrategy) field.get(null));
|
builder.propertyNamingStrategy(
|
||||||
|
(PropertyNamingStrategy) field.get(null));
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
throw new IllegalStateException(ex);
|
throw new IllegalStateException(ex);
|
||||||
|
@ -297,7 +328,8 @@ public class JacksonAutoConfiguration {
|
||||||
private void configureModules(Jackson2ObjectMapperBuilder builder) {
|
private void configureModules(Jackson2ObjectMapperBuilder builder) {
|
||||||
Collection<Module> moduleBeans = getBeans(this.applicationContext,
|
Collection<Module> moduleBeans = getBeans(this.applicationContext,
|
||||||
Module.class);
|
Module.class);
|
||||||
builder.modulesToInstall(moduleBeans.toArray(new Module[moduleBeans.size()]));
|
builder.modulesToInstall(
|
||||||
|
moduleBeans.toArray(new Module[moduleBeans.size()]));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void configureLocale(Jackson2ObjectMapperBuilder builder) {
|
private void configureLocale(Jackson2ObjectMapperBuilder builder) {
|
||||||
|
@ -316,3 +348,5 @@ public class JacksonAutoConfiguration {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -527,7 +527,8 @@ public class JacksonAutoConfigurationTests {
|
||||||
public Jackson2ObjectMapperBuilderCustomizer customDateFormat() {
|
public Jackson2ObjectMapperBuilderCustomizer customDateFormat() {
|
||||||
return new Jackson2ObjectMapperBuilderCustomizer() {
|
return new Jackson2ObjectMapperBuilderCustomizer() {
|
||||||
@Override
|
@Override
|
||||||
public void customize(Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder) {
|
public void customize(
|
||||||
|
Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder) {
|
||||||
jackson2ObjectMapperBuilder.dateFormat(new MyDateFormat());
|
jackson2ObjectMapperBuilder.dateFormat(new MyDateFormat());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue