2022-04-26 16:04:31.897 WARN 8364 --- [nio-7070-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String "2022-4-26": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2022-4-26' could not be parsed at index 5; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDateTime` from String "2022-4-26": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2022-4-26' could not be parsed at index 5<EOL> at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 4, column: 19] (through reference chain: com.example.wy.entity.Notice["releaseTime"])]
{
"timestamp": "2022-04-26T08:04:31.905+00:00",
"status": 400,
"error": "Bad Request",
"path": "/notice"
}
@JsonFormat(pattern = "yyyy-MM-dd")
但是还是没有用,有什么办法解决嘛个人喜好将下面两种注解都加上,一个是前端转后端,一个是后端转前端的。
/**
* 创建时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
//2022-03-22T18:32
@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm")
@Column(name = "create_time")
private Date createTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
"timestamp": "2022-04-26T08:04:31.905+00:00"
这一段中有个字符T,不知道会不会是这个的问题
要这样转换
LocalDateTime now = LocalDateTime.now();
String format = now.format(DateTimeFormatter.ofPattern("YYYY-MM-dd"));//格式化
System.out.println(format);
"timestamp": "2022-04-26T08:04:31.905+00:00" 这传错了。这个是时间对象, 应该传时间戳吧。 示例: "timestamp": 1650962512
@PostMapping
public Boolean save(@RequestBody Notice notice) {
return noticeService.saveOrUpdate(notice);
}
接口是这样的,releaseTime是notice类里的,这样定义的:private LocalDateTime releaseTime;
注意到这一句,显然4月前面少了0,应该是2022-04-26