提问人:ChuchiGamingYT 提问时间:10/30/2023 最后编辑:ChuchiGamingYT 更新时间:11/3/2023 访问量:79
DateTimeFormat 在 SpringBoot 3 中不会严格解析模式
DateTimeFormat does not parse the pattern strictly in SpringBoot 3
问:
下面是 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 响应代码。
我尝试使用回退模式并在回退模式属性中传递了相同的模式,它起作用了。 但我想有一个替代方案。
答:
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”)
我希望这对你有用。
评论