|
|
@ -20,6 +20,7 @@ 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.List;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.Map.Entry;
|
|
|
@ -51,6 +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.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;
|
|
|
@ -160,137 +162,189 @@ 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;
|
|
|
|
|
|
|
|
|
|
|
|
private final JacksonProperties jacksonProperties;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JacksonObjectMapperBuilderConfiguration(ApplicationContext applicationContext,
|
|
|
|
JacksonObjectMapperBuilderConfiguration(ApplicationContext applicationContext,
|
|
|
|
JacksonProperties jacksonProperties) {
|
|
|
|
JacksonProperties jacksonProperties,
|
|
|
|
|
|
|
|
List<Jackson2ObjectMapperBuilderCustomizer> customizers) {
|
|
|
|
this.applicationContext = applicationContext;
|
|
|
|
this.applicationContext = applicationContext;
|
|
|
|
this.jacksonProperties = jacksonProperties;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
@ConditionalOnMissingBean(Jackson2ObjectMapperBuilder.class)
|
|
|
|
@ConditionalOnMissingBean(Jackson2ObjectMapperBuilder.class)
|
|
|
|
public Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder() {
|
|
|
|
public Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder(
|
|
|
|
|
|
|
|
List<Jackson2ObjectMapperBuilderCustomizer> customizers) {
|
|
|
|
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
|
|
|
|
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
|
|
|
|
builder.applicationContext(this.applicationContext);
|
|
|
|
builder.applicationContext(this.applicationContext);
|
|
|
|
if (this.jacksonProperties.getDefaultPropertyInclusion() != null) {
|
|
|
|
customize(builder, customizers);
|
|
|
|
builder.serializationInclusion(
|
|
|
|
|
|
|
|
this.jacksonProperties.getDefaultPropertyInclusion());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.jacksonProperties.getTimeZone() != null) {
|
|
|
|
|
|
|
|
builder.timeZone(this.jacksonProperties.getTimeZone());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
configureFeatures(builder, this.jacksonProperties.getDeserialization());
|
|
|
|
|
|
|
|
configureFeatures(builder, this.jacksonProperties.getSerialization());
|
|
|
|
|
|
|
|
configureFeatures(builder, this.jacksonProperties.getMapper());
|
|
|
|
|
|
|
|
configureFeatures(builder, this.jacksonProperties.getParser());
|
|
|
|
|
|
|
|
configureFeatures(builder, this.jacksonProperties.getGenerator());
|
|
|
|
|
|
|
|
configureDateFormat(builder);
|
|
|
|
|
|
|
|
configurePropertyNamingStrategy(builder);
|
|
|
|
|
|
|
|
configureModules(builder);
|
|
|
|
|
|
|
|
configureLocale(builder);
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void configureFeatures(Jackson2ObjectMapperBuilder builder,
|
|
|
|
private void customize(Jackson2ObjectMapperBuilder builder,
|
|
|
|
Map<?, Boolean> features) {
|
|
|
|
List<Jackson2ObjectMapperBuilderCustomizer> customizers) {
|
|
|
|
for (Entry<?, Boolean> entry : features.entrySet()) {
|
|
|
|
for (Jackson2ObjectMapperBuilderCustomizer customizer : customizers) {
|
|
|
|
if (entry.getValue() != null && entry.getValue()) {
|
|
|
|
customizer.customize(builder);
|
|
|
|
builder.featuresToEnable(entry.getKey());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
builder.featuresToDisable(entry.getKey());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void configureDateFormat(Jackson2ObjectMapperBuilder builder) {
|
|
|
|
}
|
|
|
|
// We support a fully qualified class name extending DateFormat or a date
|
|
|
|
|
|
|
|
// pattern string value
|
|
|
|
@Configuration
|
|
|
|
String dateFormat = this.jacksonProperties.getDateFormat();
|
|
|
|
@ConditionalOnClass({ ObjectMapper.class, Jackson2ObjectMapperBuilder.class })
|
|
|
|
if (dateFormat != null) {
|
|
|
|
@EnableConfigurationProperties(JacksonProperties.class)
|
|
|
|
try {
|
|
|
|
static class Jackson2ObjectMapperBuilderCustomizerConfiguration {
|
|
|
|
Class<?> dateFormatClass = ClassUtils.forName(dateFormat, null);
|
|
|
|
|
|
|
|
builder.dateFormat(
|
|
|
|
@Bean
|
|
|
|
(DateFormat) BeanUtils.instantiateClass(dateFormatClass));
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StandardJackson2ObjectMapperBuilderCustomizer(
|
|
|
|
|
|
|
|
ApplicationContext applicationContext,
|
|
|
|
|
|
|
|
JacksonProperties jacksonProperties) {
|
|
|
|
|
|
|
|
this.applicationContext = applicationContext;
|
|
|
|
|
|
|
|
this.jacksonProperties = jacksonProperties;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public int getOrder() {
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void customize(Jackson2ObjectMapperBuilder builder) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (this.jacksonProperties.getDefaultPropertyInclusion() != null) {
|
|
|
|
|
|
|
|
builder.serializationInclusion(
|
|
|
|
|
|
|
|
this.jacksonProperties.getDefaultPropertyInclusion());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (ClassNotFoundException ex) {
|
|
|
|
if (this.jacksonProperties.getTimeZone() != null) {
|
|
|
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
|
|
|
|
builder.timeZone(this.jacksonProperties.getTimeZone());
|
|
|
|
// 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
|
|
|
|
configureFeatures(builder, this.jacksonProperties.getDeserialization());
|
|
|
|
TimeZone timeZone = this.jacksonProperties.getTimeZone();
|
|
|
|
configureFeatures(builder, this.jacksonProperties.getSerialization());
|
|
|
|
if (timeZone == null) {
|
|
|
|
configureFeatures(builder, this.jacksonProperties.getMapper());
|
|
|
|
timeZone = new ObjectMapper().getSerializationConfig()
|
|
|
|
configureFeatures(builder, this.jacksonProperties.getParser());
|
|
|
|
.getTimeZone();
|
|
|
|
configureFeatures(builder, this.jacksonProperties.getGenerator());
|
|
|
|
|
|
|
|
configureDateFormat(builder);
|
|
|
|
|
|
|
|
configurePropertyNamingStrategy(builder);
|
|
|
|
|
|
|
|
configureModules(builder);
|
|
|
|
|
|
|
|
configureLocale(builder);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void configureFeatures(Jackson2ObjectMapperBuilder builder,
|
|
|
|
|
|
|
|
Map<?, Boolean> features) {
|
|
|
|
|
|
|
|
for (Entry<?, Boolean> entry : features.entrySet()) {
|
|
|
|
|
|
|
|
if (entry.getValue() != null && entry.getValue()) {
|
|
|
|
|
|
|
|
builder.featuresToEnable(entry.getKey());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
builder.featuresToDisable(entry.getKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
simpleDateFormat.setTimeZone(timeZone);
|
|
|
|
|
|
|
|
builder.dateFormat(simpleDateFormat);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void configurePropertyNamingStrategy(
|
|
|
|
private void configureDateFormat(Jackson2ObjectMapperBuilder builder) {
|
|
|
|
Jackson2ObjectMapperBuilder builder) {
|
|
|
|
// We support a fully qualified class name extending DateFormat or a date
|
|
|
|
// We support a fully qualified class name extending Jackson's
|
|
|
|
// pattern string value
|
|
|
|
// PropertyNamingStrategy or a string value corresponding to the constant
|
|
|
|
String dateFormat = this.jacksonProperties.getDateFormat();
|
|
|
|
// names in PropertyNamingStrategy which hold default provided implementations
|
|
|
|
if (dateFormat != null) {
|
|
|
|
String strategy = this.jacksonProperties.getPropertyNamingStrategy();
|
|
|
|
try {
|
|
|
|
if (strategy != null) {
|
|
|
|
Class<?> dateFormatClass = ClassUtils.forName(dateFormat, null);
|
|
|
|
try {
|
|
|
|
builder.dateFormat(
|
|
|
|
configurePropertyNamingStrategyClass(builder,
|
|
|
|
(DateFormat) BeanUtils.instantiateClass(dateFormatClass));
|
|
|
|
ClassUtils.forName(strategy, null));
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (ClassNotFoundException ex) {
|
|
|
|
|
|
|
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
|
|
|
|
|
|
|
|
dateFormat);
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
if (timeZone == null) {
|
|
|
|
|
|
|
|
timeZone = new ObjectMapper().getSerializationConfig()
|
|
|
|
|
|
|
|
.getTimeZone();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
simpleDateFormat.setTimeZone(timeZone);
|
|
|
|
|
|
|
|
builder.dateFormat(simpleDateFormat);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (ClassNotFoundException ex) {
|
|
|
|
}
|
|
|
|
configurePropertyNamingStrategyField(builder, strategy);
|
|
|
|
|
|
|
|
|
|
|
|
private void configurePropertyNamingStrategy(
|
|
|
|
|
|
|
|
Jackson2ObjectMapperBuilder builder) {
|
|
|
|
|
|
|
|
// We support a fully qualified class name extending Jackson's
|
|
|
|
|
|
|
|
// PropertyNamingStrategy or a string value corresponding to the constant
|
|
|
|
|
|
|
|
// names in PropertyNamingStrategy which hold default provided
|
|
|
|
|
|
|
|
// implementations
|
|
|
|
|
|
|
|
String strategy = this.jacksonProperties.getPropertyNamingStrategy();
|
|
|
|
|
|
|
|
if (strategy != null) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
configurePropertyNamingStrategyClass(builder,
|
|
|
|
|
|
|
|
ClassUtils.forName(strategy, null));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (ClassNotFoundException ex) {
|
|
|
|
|
|
|
|
configurePropertyNamingStrategyField(builder, strategy);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void configurePropertyNamingStrategyClass(
|
|
|
|
private void configurePropertyNamingStrategyClass(
|
|
|
|
Jackson2ObjectMapperBuilder builder,
|
|
|
|
Jackson2ObjectMapperBuilder builder,
|
|
|
|
Class<?> propertyNamingStrategyClass) {
|
|
|
|
Class<?> propertyNamingStrategyClass) {
|
|
|
|
builder.propertyNamingStrategy((PropertyNamingStrategy) BeanUtils
|
|
|
|
builder.propertyNamingStrategy((PropertyNamingStrategy) BeanUtils
|
|
|
|
.instantiateClass(propertyNamingStrategyClass));
|
|
|
|
.instantiateClass(propertyNamingStrategyClass));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void configurePropertyNamingStrategyField(
|
|
|
|
private void configurePropertyNamingStrategyField(
|
|
|
|
Jackson2ObjectMapperBuilder builder, String fieldName) {
|
|
|
|
Jackson2ObjectMapperBuilder builder, String fieldName) {
|
|
|
|
// Find the field (this way we automatically support new constants
|
|
|
|
// Find the field (this way we automatically support new constants
|
|
|
|
// that may be added by Jackson in the future)
|
|
|
|
// that may be added by Jackson in the future)
|
|
|
|
Field field = ReflectionUtils.findField(PropertyNamingStrategy.class,
|
|
|
|
Field field = ReflectionUtils.findField(PropertyNamingStrategy.class,
|
|
|
|
fieldName, PropertyNamingStrategy.class);
|
|
|
|
fieldName, PropertyNamingStrategy.class);
|
|
|
|
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) {
|
|
|
|
|
|
|
|
throw new IllegalStateException(ex);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
|
|
|
throw new IllegalStateException(ex);
|
|
|
|
private void configureModules(Jackson2ObjectMapperBuilder builder) {
|
|
|
|
|
|
|
|
Collection<Module> moduleBeans = getBeans(this.applicationContext,
|
|
|
|
|
|
|
|
Module.class);
|
|
|
|
|
|
|
|
builder.modulesToInstall(
|
|
|
|
|
|
|
|
moduleBeans.toArray(new Module[moduleBeans.size()]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void configureModules(Jackson2ObjectMapperBuilder builder) {
|
|
|
|
private void configureLocale(Jackson2ObjectMapperBuilder builder) {
|
|
|
|
Collection<Module> moduleBeans = getBeans(this.applicationContext,
|
|
|
|
Locale locale = this.jacksonProperties.getLocale();
|
|
|
|
Module.class);
|
|
|
|
if (locale != null) {
|
|
|
|
builder.modulesToInstall(moduleBeans.toArray(new Module[moduleBeans.size()]));
|
|
|
|
builder.locale(locale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void configureLocale(Jackson2ObjectMapperBuilder builder) {
|
|
|
|
private static <T> Collection<T> getBeans(ListableBeanFactory beanFactory,
|
|
|
|
Locale locale = this.jacksonProperties.getLocale();
|
|
|
|
Class<T> type) {
|
|
|
|
if (locale != null) {
|
|
|
|
return BeanFactoryUtils.beansOfTypeIncludingAncestors(beanFactory, type)
|
|
|
|
builder.locale(locale);
|
|
|
|
.values();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static <T> Collection<T> getBeans(ListableBeanFactory beanFactory,
|
|
|
|
|
|
|
|
Class<T> type) {
|
|
|
|
|
|
|
|
return BeanFactoryUtils.beansOfTypeIncludingAncestors(beanFactory, type)
|
|
|
|
|
|
|
|
.values();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|