【发布时间】:2019-11-08 00:26:07
【问题描述】:
我有需要反序列化的带有日期参数的 JSON 正文
关注Working with Date Parameters in Spring
使用@DateTimeFormat注解对参数进行注解,并提供格式化模式参数:
我们也可以使用自己的转换模式。我们可以在@DateTimeFormat 注解中提供一个模式参数:
@PostMapping("/date") public void date(@RequestParam("date") @DateTimeFormat(pattern = "dd.MM.yyyy") Date date) {
我创建了一个 POJO
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(Include.NON_NULL)
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RequestVO {
@DateTimeFormat(pattern = "dd.MM.yyyy hh:mm:ss")
Date startDate;
我的 RestController 端点
@PostMapping(value = "path", consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String updateDate(RequestVO requestVO) {
logger.debug("requestVO.getStartDate()=" + requestVO.getStartDate());
我发布数据:
{"startDate":"11.11.2019 11:11:11"}
但我的 startDate 为空
- 其他参数工作正常(日期除外)
我可以在 Object 中使用@DateTimeFormat 还是必须像示例中那样声明所有参数?
【问题讨论】:
-
尝试将
Date更改为LocalDateTime -
@MaruthiAdithya 不幸的是与
LocalDateTime的结果相同 -
在你的restController上,尝试在RequestVO之前添加@RequestBody
updateDate(@RequestBody RequestVO requestVO) { -
你有没有试过@user7294900
-
@NaFelixWimpyWijaya 是的,请参阅 avi 答案的评论
标签: java spring datetime spring-restcontroller