DateTimeFormat does not parse the pattern strictly in SpringBoot 3

136 Views Asked by At

Below is the api :

@PostMapping(value = "/transactions")
void createTransaction(@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm") LocalDateTime startDateTime,
                                        @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm") LocalDateTime endDateTime)

Sample date : 2022-02-02T15:12:23 I have upgraded from Spring Boot 2.7 to Spring Boot 3.1

If I pass the sample date in request param, I get a 200 response code. The same date if passed in Spring Boot 2 fetches 400 status code.

What can be done to maintain the old behaviour with spring boot 3?

I would expect a 400 response code for this.

I tried to use the fallback pattern and passed the same pattern in the fallbackpattern attribute, and it worked. But I would like to have an alternative for this.

1

There are 1 best solutions below

0
On

I format LocalDateTime in spring boot 2.x.x use same as you @DateTimeFormat()

but when I upgrade to spring boot 3.1.1, I needs to change my annotation from @DateTimeFormat() to @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss")

I hope this is work for you.