jackson.date-format不生效的问题

 

版本

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
</parent>
            

描述

springboot项目中出参为json时,日期格式化配置一般为

spring.jackson.time-zone=GMT+8
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss

但是不生效,返回的是依旧是时间戳格式;

原因

添加拦截器并继承 WebMvcConfigurationSupport 后会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置!从而导致所有的Date返回都变成时间戳。

https://www.cnblogs.com/sufferingStriver/p/9026764.html

解决

 1 package com.sitech.pgcenter.config;
 2  
 3 import com.fasterxml.jackson.databind.DeserializationFeature;
 4 import com.fasterxml.jackson.databind.ObjectMapper;
 5 import com.fasterxml.jackson.databind.module.SimpleModule;
 6 import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
 7 import org.springframework.context.annotation.Configuration;
 8 import org.springframework.http.converter.HttpMessageConverter;
 9 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
10 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
11  
12 import java.text.SimpleDateFormat;
13 import java.util.List;
14  
15 /**
16  * @Description 解决springboot高版本下日期转json时jackson方式不生效问题
17  *
18  */
19 @Configuration
20 public class DateFormatForJson implements WebMvcConfigurer {
21     /**
22      * 使用此方法, 以下 spring-boot: jackson时间格式化 配置 将会失效
23      * spring.jackson.time-zone=GMT+8
24      * spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
25      * 原因: 会覆盖 @EnableAutoConfiguration 关于 WebMvcAutoConfiguration 的配置
26      * */
27     @Override
28     public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
29         MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
30         ObjectMapper objectMapper = converter.getObjectMapper();
31         // 生成JSON时,将所有Long转换成String
32         SimpleModule simpleModule = new SimpleModule();
33         simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
34         simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
35         objectMapper.registerModule(simpleModule);
36         // 时间格式化
37         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
38         objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
39         // 设置格式化内容
40         converter.setObjectMapper(objectMapper);
41         converters.add(0, converter);
42     }
43  
44 }

jackson的推荐配注释说明:

spring: 
  jackson: 
    # 设置属性命名策略,对应jackson下PropertyNamingStrategy中的常量值,SNAKE_CASE-返回的json驼峰式转下划线,json body下划线传到后端自动转驼峰式 
    property-naming-strategy: SNAKE_CASE
    
    # 全局设置@JsonFormat的格式pattern 
    date-format: yyyy-MM-dd HH:mm:ss 
    
    # 当地时区 
    locale: zh 
    
    # 设置全局时区 
    time-zone: GMT+8 
    
    # 常用,全局设置pojo或被@JsonInclude注解的属性的序列化方式 
    default-property-inclusion: NON_NULL #不为空的属性才会序列化,具体属性可看JsonInclude.Include 
    
    # 常规默认,枚举类SerializationFeature中的枚举属性为key,值为boolean设置jackson序列化特性,具体key请看SerializationFeature源码 
    serialization: 
      WRITE_DATES_AS_TIMESTAMPS: true # 返回的java.util.date转换成timestamp 
      FAIL_ON_EMPTY_BEANS: true # 对象为空时是否报错,默认true 
    
    # 枚举类DeserializationFeature中的枚举属性为key,值为boolean设置jackson反序列化特性,具体key请看DeserializationFeature源码 
    deserialization: 
      # 常用,json中含pojo不存在属性时是否失败报错,默认true 
      FAIL_ON_UNKNOWN_PROPERTIES: false 
    
    # 枚举类MapperFeature中的枚举属性为key,值为boolean设置jackson ObjectMapper特性 
    # ObjectMapper在jackson中负责json的读写、json与pojo的互转、json tree的互转,具体特性请看MapperFeature,常规默认即可 
    mapper: 
      # 使用getter取代setter探测属性,如类中含getName()但不包含name属性与setName(),传输的vo json格式模板中依旧含name属性 
      USE_GETTERS_AS_SETTERS: true #默认false
    
    # 枚举类JsonParser.Feature枚举类中的枚举属性为key,值为boolean设置jackson JsonParser特性 
    # JsonParser在jackson中负责json内容的读取,具体特性请看JsonParser.Feature,一般无需设置默认即可 
    parser: 
      ALLOW_SINGLE_QUOTES: true # 是否允许出现单引号,默认false 
    
    # 枚举类JsonGenerator.Feature枚举类中的枚举属性为key,值为boolean设置jackson JsonGenerator特性,一般无需设置默认即可 
    # JsonGenerator在jackson中负责编写json内容,具体特性请看JsonGenerator.Feature
原文地址:https://www.cnblogs.com/yucongblog/p/14107555.html