使用element-plus的el-date-picker組件時發現默認情況下,組件接受並返回Date
對象。
后台使用@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss'")進行參數綁定發現不能接收
Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.util.Date] for value '2020-12-30T14:30:49.000Z'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2020-12-30T14:30:49.000Z]]
解決方法:
這個時候發現原因為匹配格式不正確,改為@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss'.000Z'")
@PostMapping("user/freeze")
@PreAuthorize("hasAuthority('authority:user:freeze')")
public ResponseEntity userFreeze(String username, @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss'.000Z'") Date freezeDate) {
userService.userFreeze(username, freezeDate);
return ResponseEntity.success();
}
於是就能正確接收了。
使用實體類綁定參數時,注解添加至字段上即可;請求content-type為application/json時可以使用@JsonFormat注解。
T是自定義的字符串,可以替換為其他任何的字符串。Z是表示時區,SSS是毫秒數