org.zodiac.autoconfigure.jackson.PlatformJacksonOptionAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.jackson;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.jackson.JacksonProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.zodiac.commons.json.jackson.PlatformJavaTimeModule;
import org.zodiac.commons.util.DateTimes;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Optional;
import java.util.TimeZone;
@SpringBootConfiguration
@AutoConfigureBefore(value = {JacksonAutoConfiguration.class})
@ConditionalOnClass(value = {com.fasterxml.jackson.databind.ObjectMapper.class})
public class PlatformJacksonOptionAutoConfiguration {
private final JacksonProperties jacksonProperties;
public PlatformJacksonOptionAutoConfiguration(JacksonProperties jacksonProperties) {
this.jacksonProperties = jacksonProperties;
}
@Bean
@ConditionalOnMissingBean
@ConfigurationProperties(prefix = org.zodiac.core.jackson.constants.PlatformJacksonSystemPropertiesConstants.SPRING_JACKSON_OPTION_PREFIX, ignoreInvalidFields = true)
protected PlatformJacksonOptionProperties platformJacksonOptionProperties() {
return new PlatformJacksonOptionProperties();
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(value = {org.springframework.http.HttpMessage.class, com.fasterxml.jackson.databind.module.SimpleModule.class, com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer.class})
protected com.fasterxml.jackson.databind.ObjectMapper objectMapper(PlatformJacksonOptionProperties platformJacksonOptionProperties,
org.springframework.http.converter.json.Jackson2ObjectMapperBuilder builder) {
builder.simpleDateFormat(DateTimes.FORMAT_19);
/*创建ObjectMapper*/
com.fasterxml.jackson.databind.ObjectMapper objectMapper = builder.createXmlMapper(platformJacksonOptionProperties.isDefaultXmlMapper()).build();
/*设置地点*/
//objectMapper.setLocale(Locale.CHINA);
Locale jacksonLocale = Optional.ofNullable(jacksonProperties.getLocale()).orElseGet(() -> Locale.forLanguageTag(platformJacksonOptionProperties.getLocaleName()));
objectMapper.setLocale(jacksonLocale);
/*去掉默认的时间戳格式*/
objectMapper.configure(com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, platformJacksonOptionProperties.isWriteDatesAsTimestamps());
/*设置时区*/
TimeZone jacksonTimeZone = Optional.ofNullable(jacksonProperties.getTimeZone()).orElseGet(() -> TimeZone.getTimeZone(platformJacksonOptionProperties.getTimeZoneId()));
objectMapper.setTimeZone(jacksonTimeZone);
/*序列化时,日期的统一格式*/
String jacksonDateFormat = Optional.ofNullable(jacksonProperties.getDateFormat()).orElseGet(() -> platformJacksonOptionProperties.getDateFormat());
objectMapper.setDateFormat(new SimpleDateFormat(jacksonDateFormat, jacksonLocale));
/*序列化处理*/
objectMapper.configure(com.fasterxml.jackson.core.json.JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature(), platformJacksonOptionProperties.isUnescapedControlChars());
objectMapper.configure(com.fasterxml.jackson.core.json.JsonReadFeature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER.mappedFeature(), platformJacksonOptionProperties.isBackslashEscapingAnyChars());
/*失败处理*/
objectMapper.configure(com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_EMPTY_BEANS, platformJacksonOptionProperties.isFailOnEmptyBeans());
objectMapper.configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, platformJacksonOptionProperties.isFailOnUnknownProperties());
/*单引号处理*/
objectMapper.configure(com.fasterxml.jackson.core.json.JsonReadFeature.ALLOW_SINGLE_QUOTES.mappedFeature(), platformJacksonOptionProperties.isAllowSingleQuotes());
/*反序列化时,属性不存在的兼容处理*/
objectMapper.getDeserializationConfig().withoutFeatures(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
/*日期格式化*/
objectMapper.registerModule(PlatformJavaTimeModule.INSTANCE);
objectMapper.findAndRegisterModules();
return objectMapper;
}
}