简体   繁体   English

对于使用@Temporal注释的日期序列化,spring.jackson.date-format属性无法正常工作

[英]spring.jackson.date-format property does not work properly for the date serialization with @Temporal annotation

I have the following property in application.properties file: 我在application.properties文件中有以下属性:

spring.jackson.date-format=yyyy-MMM-dd

There is object definition to be serialized: 有序列化的对象定义:

public class InjuryDTO {

private Long id;

private String kindOfInjury;

private String muscle;

private String side;

private Integer outOfTraining;

private Date injuryDate;

private Long athleteId;

// getters and setters are omitted for brevity }

This is class from which InjuryDTO object is originally created: 这是最初创建InjuryDTO对象的类:

@Entity
@Table(name = "INJURY")
public class Injury {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "INJURY_ID")
private Long id;

@Column(name = "KIND_OF_INJURY")
private String kindOfInjury;

@Column(name = "MUSCLE")
private String muscle;

@Column(name = "SIDE")
private String side;

@Column(name = "OUT_OF_TRAINING")
private Integer outOfTraining;

@Temporal(value = TemporalType.DATE)
@Column(name = "INJURY_DATE")
private Date injuryDate;

@ManyToOne
@JoinColumn(name = "ATHLETE_ID")
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private Athlete athlete;

// get,set-ters are removed for brevity 
}

So, if the deserialization of this JSON property happens: 因此,如果发生此JSON属性的反序列化:

"injuryDate":"2018-Jun-02"

Jackson accepts this string and transforms it to the corresponding java.util.Date object, but when serialization happens with no commented @Temporal(value = TemporalType.DATE) annotation then server gets back the following JSON property: "injuryDate":"2018-06-02" . Jackson接受此字符串并将其转换为相应的java.util.Date对象,但是当序列化发生时没有注释@Temporal(value = TemporalType.DATE)注释时,服务器将返回以下JSON属性: "injuryDate":"2018-06-02"

Question is : Why @Temporal annotation affects the actual representation of the date property in JSON? 问题是 :为什么@Temporal注释会影响JSON中date属性的实际表示?

Try this one: 试试这个:

 @Temporal(TemporalType.DATE)
 @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
 @Column(name="INJURY_DATE")
 private Date injuryDate;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM