DateTimeFormat 在 SpringBoot 3 中不会严格解析模式

DateTimeFormat does not parse the pattern strictly in SpringBoot 3

提问人:ChuchiGamingYT 提问时间:10/30/2023 最后编辑:ChuchiGamingYT 更新时间:11/3/2023 访问量:79

问:

下面是 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)

示例日期:2022-02-02T15:12:23 我已从 Spring Boot 2.7 升级到 Spring Boot 3.1

如果我在请求参数中传递示例日期,我会得到一个 200 响应代码。 如果在 Spring Boot 2 中传递相同的日期,则获取 400 状态代码。

如何保持 Spring Boot 3 的旧行为?

我希望有一个 400 响应代码。

我尝试使用回退模式并在回退模式属性中传递了相同的模式,它起作用了。 但我想有一个替代方案。

java 日期时间 spring-boot-3

评论

0赞 Ryuzaki L 10/30/2023
你能在升级之前和升级后显示 200 和 400 的请求和响应吗?

答:

1赞 PoYu 11/3/2023 #1

我在 spring boot 2.x.x 中使用与您相同的格式 LocalDateTime @DateTimeFormat()

但是当我升级到 Spring Boot 3.1.1 时,我需要将我的注释从 @DateTimeFormat() 更改为 @JsonFormat(pattern = “yyyy-MM-dd hh:mm:ss”)

我希望这对你有用。