由于LocalDateTime返回时时间中包含T的情况,需要对时间进行格式化
分析三种时间格式化注解的使用场景
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;
import com.alibaba.fastjson.annotation.JSONField;
public class User {
@JsonFormat(pattern = LocalDateTimeUtils.YYYY_MM_DD_HH_MM_SS)
@DateTimeFormat(pattern = LocalDateTimeUtils.YYYY_MM_DD_HH_MM_SS)
@JSONField(format = LocalDateTimeUtils.YYYY_MM_DD_HH_MM_SS)
private LocalDateTime createTime;
}
- @JsonFormat 主要用于直接由spring框架返回的格式化,是后端返回数据的格式化
- @DateTimeFormat 主要用于后端接收前端时间的格式化,指定接收某种时间格式的入参
- @JSONField 主要用于对实体进行转换JSON格式的格式化,特定为对对象使用了com.alibaba.fastjson.JSONObject.toJSONString(entity); 方法,是后端返回数据的格式化