@Data
public class Reponse {
private String event;
@Temporal(TemporalType.TIMESTAMP)
private Date eventDate;
private Double amount;
}
Json的回应就像
{
event: "transcation',
eventDate: 1213123434,
amount: 100
}
在这里,eventDate显示的是数值1540317600000,而不是2018-10-23
答案 0 :(得分:3)
您可以使用@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm")
对该字段进行注释。然后,响应时间格式将类似于“ yyyy-MM-dd HH:mm
”
import com.fasterxml.jackson.annotation.JsonFormat;
public class Reponse {
private String event;
@Temporal(TemporalType.TIMESTAMP)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm")
private Date eventDate;
private Double amount;
}
答案 1 :(得分:1)
如果您使用Spring Boot 2.x而不是1.x,则默认行为已更改
将spring.jackson.serialization.write-dates-as-timestamps=true
添加到您的配置中以返回到以前的行为
Spring Boot 2.0 Migration Guide
答案 2 :(得分:0)
I suppose you are using rest framework such as spring boot or jersey which in turn
converts your java date into epoch format before sending it to the client. So while
sending response you can format you date into the format you want. Please refer
the code below.
import java.text.SimpleDateFormat;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
sdf.setLenient(false);
String responseDate = sdf.format(date);
答案 3 :(得分:0)
spring 2.x翻转了Jackson的默认配置以将JSR-310日期写为ISO-8601字符串。如果您希望返回到以前的行为,则可以添加
spring.jackson.serialization.write-dates-as-timestamps=true
到您的应用程序上下文配置文件。