springboot 返回json格式数据时间格式配置

方法一:

可以在apllication.property加入下面配置就可以

#时间戳统一转换
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss

spring.jackson.time-zone=GMT+8

 

方法二:

@JsonFormat(timezone = "GMT+8", pattern = "yyyyMMddHHmmss")

private Date createTime;

 

 

 

如果application.properties中的如下配置不生效,返回时间戳

原因分析:

       拦截器继承的 WebMvcConfigurationSupport !

       以前是用 WebMvcConfigurerAdapter ,springboot 2.0 建议使用 WebMvcConfigurationSupport 。但是在添加拦截器并继承 WebMvcConfigurationSupport 后会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置!从而导致所有的Date返回都变成时间戳!

解决方法:

       实现 WebMvcConfigurer。

       将:

extends WebMvcConfigurationSupport
       改为:

implements WebMvcConfigurer
 

你可能感兴趣的:(springboot 返回json格式数据时间格式配置)