当@ResponseBody返回字段类型定义为LocalDateTime、LocalDate时返回给前端的数据没有被格式化,加上@JsonFormat(pattern = "yyyy-MM-dd")依然无法转换, 但如果字段类型定义为Date时,却可以转换成功。
@com.fasterxml.jackson.databind.annotation.JsonSerialize(using = com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer.class)
@com.fasterxml.jackson.annotation.JsonFormat(pattern = "yyyy-MM-dd")
private LocalDateTime lastDate;
@JsonSerialize(using = com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer.class)
private LocalDate lastDate2;
@JsonFormat(pattern = "yyyy-MM-dd")
private Date lastDate3;
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.8.5</version>
</dependency>