使用Payara 4.174并在后端接收较长的日期,例如1540545780000。现在,我升级到Payara版本5。
public User {
String name;
Date birthdate;
}
@POST
@Path("/user/)
public void createUser(User user) {
...
}
json call
{
name: "name",
birthdate: 1540545780000
}
现在,呼叫因以下错误而刹车:
Caused by: javax.json.bind.JsonbException: Error parsing class java.util.Date from value: 1540545780000. Check your @JsonbDateFormat has all time units for class java.util.Date type, or consider using org.eclipse.yasson.YassonProperties#ZERO_TIME_PARSE_DEFAULTING.
at org.eclipse.yasson.internal.serializer.AbstractDateTimeDeserializer.deserialize(AbstractDateTimeDeserializer.java:70)
at org.eclipse.yasson.internal.serializer.AbstractContainerDeserializer.deserializeInternal(AbstractContainerDeserializer.java:85)
at org.eclipse.yasson.internal.serializer.AbstractContainerDeserializer.deserialize(AbstractContainerDeserializer.java:61)
at org.eclipse.yasson.internal.Unmarshaller.deserializeItem(Unmarshaller.java:62)
at org.eclipse.yasson.internal.Unmarshaller.deserialize(Unmarshaller.java:52)
at org.eclipse.yasson.internal.JsonBinding.deserialize(JsonBinding.java:45)
at org.eclipse.yasson.internal.JsonBinding.fromJson(JsonBinding.java:85)
at org.glassfish.jersey.jsonb.internal.JsonBindingProvider.readFrom(JsonBindingProvider.java:99)
... 62 common frames omitted
Caused by: java.time.format.DateTimeParseException: Text '1540545780000' could not be parsed at index 0
答案 0 :(得分:1)
这是因为Payara 5切换为使用Yasson作为其JsonB提供程序,该提供程序长时间不包含映射器,java.util.Date
(请参见here)。您必须编写自己的版本才能将数字映射为Date
格式。
答案 1 :(得分:0)
乔纳森部分正确。您不必编写自定义DateTypeDeserializer即可解决您的问题。但是,如错误消息所述,您可以使用@JsonbDateFormat注释字段以解决错误,您必须确保它具有所有时间单位。
这里是一个例子:
public User {
String name;
@JsonbDateFormat(value = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
Date birthdate;
}