问题
Using Payara 4.174 and receive in backend the date as a long, for example 1540545780000. Now, I upgraded to Payara version 5.
public User {
String name;
Date birthdate;
}
@POST
@Path("/user/)
public void createUser(User user) {
...
}
json call
{
name: "name",
birthdate: 1540545780000
}
Now The call brakes with the following error:
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
回答1:
Jonathan is partly right. You don't have to write a custom DateTypeDeserializer to solve your issue. However, as the error message describes, you can annotate your Field with @JsonbDateFormat to resolve your error, you have to make sure it has all time units.
Here is an example:
public User {
String name;
@JsonbDateFormat(value = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
Date birthdate;
}
回答2:
This is because Payara 5 switched to using Yasson as its JsonB provider, which does not contain a mapper for long to java.util.Date
(see here). You have to write your own version to map a number to Date
format.
来源:https://stackoverflow.com/questions/53118216/date-format-changed-in-payara-5-long-does-not-work-anymore-org-eclipse-yas