In my spring boot application I have booking controller which has corresponding service,repository and controller. My booking Model looks like this :
@JsonFormat(pattern = "yyyy-MM-dd")
@Column(name = "datetime")// corresponds to value
private Date date;
public Date getDatetime() {
return this.date;
}
public void setDateTime(Date dateTime) {
this.date = dateTime;
}
Controller
@GetMapping("api/booking_details/{userEmail}")
@ResponseBody
public ResponseEntity<List<Booking>> getDetails(
@PathVariable @Email String userEmail) {
return new ResponseEntity<>(bookService.findByEmail(userEmail), HttpStatus.OK);
}
My corresponding get request is
api/booking_details/
the response I am getting is :
{
"datetime": "2021-09-12T16:01:04.000 00:00",
"date": "2021-09-12"
}
Can any one let me know what could be reason for having two values in response?
CodePudding user response:
It looks like both the "date" property and the return value of the getDateTime() method are being serialized.
I wouldn't normally expect the "date" property, which is private, to get serialzied, but perhaps it is because you have the @JsonFormat annotation on it?
The getDateTIme() return value is serialized because it's name indicates that it's a Java Bean property.
If what you are looking for is just the formatted date, I'd try moving the @JsonFormat annotation to the method.
CodePudding user response:
The problem is getter and setter method are not identified as date parameter's
getter and setter methods. Two things you can do,
Rename getter and setter method name as follows,
public Date getDate() {
return this.date;
}
public void setDate(Date date) {
this.date = date;
}
Or annotate getter and setter method using @JsonProperty("date")
annotation. Added this annotation for only getter method is also sufficient.
@JsonProperty("date")
public Date getDatetime() {
return this.date;
}
@JsonProperty("date")
public void setDateTime(Date dateTime) {
this.date = dateTime;
}