我们在数据库中是使用datetime类型,t把实体类用 @RestController 往前端返回的时候,Date 数据会默认使用 ISO 8601 格式返回 会带有时区显然这不是我们想要的
解决方案1:全局配置
配置文件中添加如下设置
spring:
jackson:
# 格式化返回时间 yyyy-MM-dd HH:mm:ss
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
或
spring:
jackson:
# 返回时间戳
serialization:
write-dates-as-timestamps: true
解决方案2:局部配置
给实体类加注解设置返回给前端的时间格式
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8") private Date date;
接收时间数据 出现string转date异常
Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date'
实体类属性加这个注解
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
浙公网安备 33010602011771号