please I need your help, I have been trying to add a Date field into DB using SpringBoot and JPA, but when I try to do it inserts the data in null, I mean, all other fields are recognized well using Json, but Date field in null. I have found other questions about it but I have not been able to solve it yet. I also tryied changing anotation above Date field and no luck. Any help is appreciated, thanks!
From my Entity "Member"
@JsonFormat(pattern="yyyy-MM-dd")
@Column(name = "birth_day")
private Date birth_day;
From my controller
@PostMapping()
@ResponseStatus(value = HttpStatus.CREATED)
public ResponseEntity<Member> save(@Validated @RequestBody Member input) {
memberRepository.save(input);
return ResponseEntity.ok().body(input);
}
This is the Json I create and pass through the body
{
"name": "test",
"last_name": "test",
"phone": "test",
"birth_day": "1991-04-05",
"home_address": "test"
}
And this is the response I get
{
"id": 12,
"name": "test",
"last_name": "test",
"phone": "test",
"birth_day": null,
"home_address": "test"
}
For what I can see, the rest of the fields created with no problem, but the field birth_day is a null DateTime in DB.
By the way I am using Mysql 5.7.22 and Java 11
CodePudding user response:
The @JsonFormat
for Date
s annotation needs also the shape
parameter as shown here. So the snippet to use is
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd")
@Column(name = "birth_day")
private Date birth_day;