无法从字符串反序列化类型为java.time.LocalDate的值

时间:2019-07-15 12:38:57

标签: java json spring-boot jpa jackson

由于以下异常,我无法反序列化Java-8-LocatDate

JSON解析错误:无法从字符串“ 15/09/1978”反序列化类型private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="EMP_ID", updatable = false, nullable = false) private long empId; @Column(name="BIRTH_DATE") //below line gave compilation error ==> Type mismatch: cannot convert from DateTimeFormatter to DateTimeFormat.ISO @DateTimeFormat(iso = DateTimeFormatter.ofPattern("dd/mm/yyyy")) @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd/mm/yyyy") private LocalDate birthDate; 的值:无法反序列化java.time.LocalDate(java.time.format .DateTimeParseException)文本'15 / 09 / 1978'无法在索引0处解析;

包含Date的JSON请求对象如下

[ “ employeeName”:“ ABC XYZ”,      “ birthDate”:“ 15/09/1978” ]

我还尝试通过引用URL Deserialize Java 8 LocalDateTime with JacksonMapper来实现日期反序列化 但是,下面的代码行

@DateTimeFormat(iso = DateTimeFormatter.ofPattern(“ dd / mm / yyyy”))给  以下编译错误

类型不匹配:无法从DateTimeFormatter转换为DateTimeFormat.ISO
在行中

{{1}}

请帮助我解决此问题, 预先感谢

1 个答案:

答案 0 :(得分:0)

我建议的是:

  

发送以毫秒为单位的日期,并以毫秒为单位更改该String的日期。

public class DateConverter {

    public static Date getDate(String sessionDate) {
        Long sessionOnDate = Long.parseLong(sessionDate);
        Date date = new Date(sessionOnDate);
        return date;
    }

    public static Long getTimeinMilliseconds(String myDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = sdf.parse(myDate);
        long millis = date.getTime();
        return millis;
    }

}

,在Entity中,您可以简单地拥有:

public class SessionDynamic
{
private Date sessionDate;
}

让我知道:)