springboot对ZonedDateTime返回结果json是string-源码分析
- application/json格式默认使用ObjectMapper实例进行序列化
- ObjectMapper自动注入分析
- springboot关于jackson配置
java.time.ZonedDateTime
application/json格式默认使用ObjectMapper实例进行序列化
controller返回后,打断点,可以看到,对应application/json,可以看到会选择ObjectMapper进行转化
converter: org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfiguration.MappingJackson2HttpMessageConverterConfiguration#mappingJackson2HttpMessageConverter
ObjectMapper自动注入分析
具体看自动配置类:spring-boot-autoconfiguration.jar的org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration
相当于:
MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
MAPPER.configure(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS, false);
所以springboot返回的时间默认不是long类型;
springboot关于jackson配置
1.配置文件:spring.jackson.time-zone=Asia/Shanghai
2.会注入到spring-boot-autoconfiguration.jar的JacksonProperties
以时区配置为例