springboot项目中返回时间序列化差8小时
查看源码发现,在springboot2.x以后,序列化配置作出以下修改
@Configuration @ConditionalOnClass(ObjectMapper.class) public class JacksonAutoConfiguration { private static final Map<?, Boolean> FEATURE_DEFAULTS; static { Map<Object, Boolean> featureDefaults = new HashMap<>(); featureDefaults.put(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); FEATURE_DEFAULTS = Collections.unmodifiableMap(featureDefaults); } }
如果
featureDefaults.put(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
则可以返回时间戳,具体配置如下:
spring: jackson: serialization: write-dates-as-timestamps: true
如果想要返回时间格式化类型,即DateFormat格式化后的结果。可以看到源码中
/** * Method for overriding default TimeZone to use for formatting. * Default value used is UTC (NOT default TimeZone of JVM). */ public ObjectMapper setTimeZone(TimeZone tz) { _deserializationConfig = _deserializationConfig.with(tz); _serializationConfig = _serializationConfig.with(tz); return this; }
即默认使用UTC,不会默认使用JVM的TimeZone
所以可以作出以下修改
spring: jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8
标签:返回,tz,springboot,featureDefaults,TimeZone,序列化 From: https://www.cnblogs.com/zhengyuanyuan/p/16899945.html