简体   繁体   中英

How to parse LocalDate with Jackson

I am facing problem passing date as json data through postman. In case of Mandetory date with @NotNull annotation, no problem. The other date is accepting null. But at the time of updation, that date creates problem. I am using Java 1.8 with spring boot & MySql DB. Please help

The following links I have visited, but does not fit with this. LocalDateTime parsing with jackson JSON parse error: Can not construct instance of java.time.LocalDate: no String-argument constructor/factory method to deserialize from String value https://stackoverflow.com/a/29959842/3415090 JSON Java 8 LocalDateTime format in Spring Boot

I have also used

@JsonSerialize(using = LocalDateSerializer.class)
@JsonDeserialize(using = LocalDateDeserializer.class)

But problem remains as it is.

My UX

public class LeaveApplUx {

    @JsonIgnore
    @Size(max = 5, message = "Employee Code Must Be Within 4 To 5 Character Long Or Blank")
    private final String employeeCode;

    @NotNull(message = "Start Date Empty")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
    private final LocalDate startDate;

    @JsonIgnore
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
    private final LocalDate rejoinDate;

    public LeaveApplUx(
            @Size(min = 4, max = 5, message = "Employee Code Must Be Within 4 To 5 Character Long Or Blank") 
        @JsonProperty("employeeCode") String employeeCode,
            @NotNull(message = "Start Date Empty") @JsonProperty("startDate") LocalDate startDate,
            @JsonProperty("rejoinDate") LocalDate rejoinDate) {
        this.employeeCode = employeeCode;
        this.startDate = startDate;
        this.rejoinDate = rejoinDate;
    }
//  GETTERS
}

At the time of creation, it works fine.

{
    "employeeCode": "B426",
    "startDate": "01-03-2023"
}

Input Parameters : {"employeeCode":"B426","startDate":{"year":2023,"month":"MARCH","monthValue":3,"dayOfMonth":1,"leapYear":false,"dayOfWeek":"WEDNESDAY","dayOfYear":60,"era":"CE","chronology":{"id":"ISO","calendarType":"iso8601"}},"rejoinDate":null} Record saved properly in DB

But at the time of updation, it creates error.

{
    "employeeCode": "B426",
    "startDate": "01-03-2023",
    "rejoinDate": "06-03-2023"
}

JSON parse error: 

Cannot deserialize value of type `java.time.LocalDate` from String "06-03-2023": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '06-03-2023' could not be parsed at index 0; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDate` from String "06-03-2023": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '06-03-2023' could not be parsed at index 0
     at [Source: (PushbackInputStream); line: 14, column: 19] (through reference chain: org.myapp.ux.hr.leave.LeaveApplUx["rejoinDate"])

Since you set values through contructor, not setters, you should put @JsonFormat(...) on constructor parameters, not fields. This should fix it:

public class LeaveApplUx {

    @JsonIgnore
    private final String employeeCode;

    private final LocalDate startDate;

    @JsonIgnore
    private final LocalDate rejoinDate;

    public LeaveApplUx(@JsonProperty("employeeCode") String employeeCode,
                       @JsonProperty("startDate") @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy") LocalDate startDate,
                       @JsonProperty("rejoinDate") @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy") LocalDate rejoinDate) {
        this.employeeCode = employeeCode;
        this.startDate = startDate;
        this.rejoinDate = rejoinDate;
    }

    //getters
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM