如何用 Jackson 解析 LocalDate
How to parse LocalDate with Jackson
我在通过邮递员将日期作为 json 数据传递时遇到问题。对于带有 @NotNull
注释的强制日期,没问题。另一个日期接受空值。但在更新时,该日期会产生问题。
我正在使用 Java 1.8 和 spring boot & MySql DB。请帮忙
以下链接我访问过,但与此不符。
JSON parse error: Can not construct instance of java.time.LocalDate: no String-argument constructor/factory method to deserialize from String value
我也用过
@JsonSerialize(using = LocalDateSerializer.class)
@JsonDeserialize(using = LocalDateDeserializer.class)
但问题依旧。
我的用户体验
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
}
创建时,运行正常
{
"employeeCode": "B426",
"startDate": "01-03-2023"
}
输入参数:{"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}
记录正确保存在数据库中
但是在更新的时候,会产生错误。
{
"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"])
由于您通过构造函数而不是 setter 设置值,因此您应该将 @JsonFormat(...)
放在构造函数参数上,而不是字段。这应该可以解决它:
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
}
我在通过邮递员将日期作为 json 数据传递时遇到问题。对于带有 @NotNull
注释的强制日期,没问题。另一个日期接受空值。但在更新时,该日期会产生问题。
我正在使用 Java 1.8 和 spring boot & MySql DB。请帮忙
以下链接我访问过,但与此不符。
我也用过
@JsonSerialize(using = LocalDateSerializer.class)
@JsonDeserialize(using = LocalDateDeserializer.class)
但问题依旧。
我的用户体验
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
}
创建时,运行正常
{
"employeeCode": "B426",
"startDate": "01-03-2023"
}
输入参数:{"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}
记录正确保存在数据库中
但是在更新的时候,会产生错误。
{
"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"])
由于您通过构造函数而不是 setter 设置值,因此您应该将 @JsonFormat(...)
放在构造函数参数上,而不是字段。这应该可以解决它:
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
}