0

I have the following property in application.properties file:

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:

@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:

"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".

Question is: Why @Temporal annotation affects the actual representation of the date property in JSON?

Maksim
  • 351
  • 1
  • 2
  • 12

1 Answers1

1

Try this one:

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