JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String “2023-03-11

  • Post author:
  • Post category:java




问题描述

在做项目时遇到问题:

在这里插入图片描述

后台报错:

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;



版权声明:本文为m0_61871870原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。