Jackson序列化(2)—|Jackson序列化(2)— [SpringBoot2.x]-Spring容器中ObjectMapper

Jackson序列化(1)— [SpringBoot2.x]-Jackson在HttpMessageConverter(消息转换器)中的使用
Jackson序列化(2)— [SpringBoot2.x]-Spring容器中ObjectMapper配置
Jackson序列化(3)— Jackson中ObjectMapper配置详解
Jackson序列化(4)— Jackson“默认的”时间格式化类—StdDateFormat解析
Jackson序列化(5) — Jackson的ObjectMapper.DefaultTyping.NON_FINAL属性
Jackson序列化(6)— Java使用Jackson进行序列化
上文说到,SpringBoot2.x会自动装载MappingJackson2HttpMessageConverter进行消息转换。而MappingJackson2HttpMessageConverter会获取Spring容器中的ObjectMapper配置,来进行Jackson的序列化和反序列化。

注意:在SpringBoot2.x环境下,不要将自定义的ObjectMapper对象放入Spring容器!这样会将原有的ObjectMapper配置覆盖。
ObjectMapper是JSON操作的核心,Jackson的JSON操作都是在ObjectMapper中实现的。
1. SpringBoot2.x中的ObjectMapper SpringBoot2.x默认装载了ObjectMapper到Spring容器,在yml配置文件中使用spring.jackson为前缀的配置可以修改,也可以在代码中实现Jackson2ObjectMapperBuilderCustomizer接口去修改ObjectMapper配置。
@Configuration //注解通俗的说就是Spring工程中引用了Jackson的包 才会构建这个bean @ConditionalOnClass(ObjectMapper.class) public class JacksonAutoConfiguration { //默认的feature配置 private static final Map FEATURE_DEFAULTS; //SpringBoot2.x配置WRITE_DATES_AS_TIMESTAMPS=false,即Date日期不会转换为时间戳类型 static { Map featureDefaults = new HashMap<>(); featureDefaults.put(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); FEATURE_DEFAULTS = Collections.unmodifiableMap(featureDefaults); }@Bean public JsonComponentModule jsonComponentModule() { return new JsonComponentModule(); }@Configuration @ConditionalOnClass(Jackson2ObjectMapperBuilder.class) static class JacksonObjectMapperConfiguration { @Bean @Primary //若自定义ObjectMapper放入Spring容器中,该配置不会生效! @ConditionalOnMissingBean //获取Spring容器中的Jackson2ObjectMapperBuilder 。 public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { return builder.createXmlMapper(false).build(); } }@Configuration @ConditionalOnClass(Jackson2ObjectMapperBuilder.class) static class JacksonObjectMapperBuilderConfiguration {private final ApplicationContext applicationContext; JacksonObjectMapperBuilderConfiguration(ApplicationContext applicationContext) { this.applicationContext = applicationContext; }@Bean //若自定义配置Jackson2ObjectMapperBuilder 对象,那么该配置不会生效 @ConditionalOnMissingBean //List会读取Spring容器中所有 //Jackson2ObjectMapperBuilderCustomizer子类Bean,并按照order优先级进行排序。 public Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder( List customizers) { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); builder.applicationContext(this.applicationContext); //按照order读取所有自定义的Jackson2ObjectMapperBuilderCustomizer 对象。 customize(builder, customizers); return builder; } //调用子类实现customize方法定制Jackson2ObjectMapperBuilder 对象 private void customize(Jackson2ObjectMapperBuilder builder, List customizers) { for (Jackson2ObjectMapperBuilderCustomizer customizer : customizers) { customizer.customize(builder); } }} //SpringBoot2.x提供的修改ObjectMapper的配置类,可以在配置文件中修改配置。 @Configuration @ConditionalOnClass(Jackson2ObjectMapperBuilder.class) //配置以`spring.jackson`作为前缀。 @EnableConfigurationProperties(JacksonProperties.class) static class Jackson2ObjectMapperBuilderCustomizerConfiguration {@Bean public StandardJackson2ObjectMapperBuilderCustomizer standardJacksonObjectMapperBuilderCustomizer( ApplicationContext applicationContext, JacksonProperties jacksonProperties) { return new StandardJackson2ObjectMapperBuilderCustomizer(applicationContext, jacksonProperties); }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; } //优先级为0,若我们想自定义实现StandardJackson2ObjectMapperBuilderCustomizer类,那么order优先级要大于0 @Override public int getOrder() { return 0; } //根据配置去定制builder对象 @Override public void customize(Jackson2ObjectMapperBuilder builder) {if (this.jacksonProperties.getDefaultPropertyInclusion() != null) { builder.serializationInclusion( this.jacksonProperties.getDefaultPropertyInclusion()); } if (this.jacksonProperties.getTimeZone() != null) { builder.timeZone(this.jacksonProperties.getTimeZone()); } //设置序列化和反序列化的特征 configureFeatures(builder, FEATURE_DEFAULTS); configureVisibility(builder, this.jacksonProperties.getVisibility()); 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); }private void configureFeatures(Jackson2ObjectMapperBuilder builder, Map features) { features.forEach((feature, value) -> { if (value != null) { if (value) { builder.featuresToEnable(feature); } else { builder.featuresToDisable(feature); } } }); }private void configureVisibility(Jackson2ObjectMapperBuilder builder, Map【Jackson序列化(2)—|Jackson序列化(2)— [SpringBoot2.x]-Spring容器中ObjectMapper】 visibilities) { visibilities.forEach(builder::visibility); } //配置Date格式化类 private void configureDateFormat(Jackson2ObjectMapperBuilder builder) { // 支持全限定名的DateFormat或者时间格式串例如`yyyy-MM-dd HH:mm:ss` String dateFormat = this.jacksonProperties.getDateFormat(); if (dateFormat != null) { try { Class dateFormatClass = ClassUtils.forName(dateFormat, null); builder.dateFormat( (DateFormat) BeanUtils.instantiateClass(dateFormatClass)); } catch (ClassNotFoundException ex) { //若不是上送DateFormat的全限定名,那么使用SimpleDateFormat 进行格式化 SimpleDateFormat simpleDateFormat = new SimpleDateFormat( dateFormat); // 在Jackson 2.6.3 我们需要设置一个 TimeZone时区,若未设置,则使用默认的时区(UTC) TimeZone timeZone = this.jacksonProperties.getTimeZone(); if (timeZone == null) { timeZone = new ObjectMapper().getSerializationConfig() .getTimeZone(); } simpleDateFormat.setTimeZone(timeZone); builder.dateFormat(simpleDateFormat); } } }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( Jackson2ObjectMapperBuilder builder, Class propertyNamingStrategyClass) { builder.propertyNamingStrategy((PropertyNamingStrategy) BeanUtils .instantiateClass(propertyNamingStrategyClass)); }private void configurePropertyNamingStrategyField( Jackson2ObjectMapperBuilder builder, String fieldName) { // Find the field (this way we automatically support new constants // that may be added by Jackson in the future) Field field = ReflectionUtils.findField(PropertyNamingStrategy.class, fieldName, PropertyNamingStrategy.class); Assert.notNull(field, () -> "Constant named '" + fieldName + "' not found on " + PropertyNamingStrategy.class.getName()); try { builder.propertyNamingStrategy( (PropertyNamingStrategy) field.get(null)); } catch (Exception ex) { throw new IllegalStateException(ex); } }private void configureModules(Jackson2ObjectMapperBuilder builder) { Collection moduleBeans = getBeans(this.applicationContext, Module.class); builder.modulesToInstall(moduleBeans.toArray(new Module[0])); }private void configureLocale(Jackson2ObjectMapperBuilder builder) { Locale locale = this.jacksonProperties.getLocale(); if (locale != null) { builder.locale(locale); } }private static Collection getBeans(ListableBeanFactory beanFactory, Class type) { return BeanFactoryUtils.beansOfTypeIncludingAncestors(beanFactory, type) .values(); } } } }

2. 修改方式 2.1 配置文件修改
spring: jackson: serialization: write-dates-as-timestamps: true# date日期转换为时间戳 date-format: yyyy-MM-dd HH:mm:ss#指定日期格式,比如yyyy-MM-dd HH:mm:ss,或者具体的格式化类的全限定名

2.2 java代码配置
@Component public class MyJackson2ObjectMapperBuilderCustomizer implements Jackson2ObjectMapperBuilderCustomizer, Ordered { @Override public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) { //禁止Date类型转化为时间戳。 jacksonObjectMapperBuilder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); }@Override public int getOrder() { return 1; } }

    推荐阅读