(016) @DateTimeFormat与@JsonForma
2018-12-23 本文已影响0人
Lindm
一、前言
当遇到前后台日期数据交互时,常遇到后台日期传至前台变成长整型数据,前端需要再一次手动转化格式;前端传至后台日期数据类型为字符串,查询不出预期结果。为简化处理该类问题,可使用@DateTimeFormat与@JsonFormat直接进行格式处理。
二、使用
1、实体类属性上直接添加注解。@DateTimeFormat把前台数据解析为Date;@JsonFormat将后台Date数据转为指定日期格式传回前台。
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;
@JsonFormat(pattern = "yyyy-MM-dd")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date draftDate;
2、mapper映射文件,可以不显示指定javaType及jdbcType,为避免空值出现的隐式格式转换异常,建议显式设置对应的格式。
<result property="draftDate" column="DRAFT_DATE" javaType="java.util.Date" jdbcType="DATE"/>