问题描述
在做项目时遇到问题:
后台报错:
Resolved [org.springframework.http.converter.HttpMessageNotReadableException:
JSON parse error: Cannot deserialize value of type
java.time.LocalDateTime
from String “2023-03-11 09:00:00”:
Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text ‘2023-03-11 09:00:00’ could not be parsed at index 10;
nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException:
Cannot deserialize value of type
java.time.LocalDateTime
from String “2023-03-11 09:00:00”:
Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text ‘2023-03-11 09:00:00’ could not be parsed at index 10 at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream);
line: 4, column: 17] (through reference chain: com.cxz.springdemo02.Entity.ProcessingBatch[“startTime”])]
问题解决:
在实体类的字段加上@JsonFormat即可
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime startTime;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime endTime;