4

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
Sorin Penteleiciuc
  • 653
  • 1
  • 10
  • 26

2 Answers2

2

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;
}
Florian
  • 385
  • 6
  • 13
1

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.

Jonathan Coustick
  • 1,127
  • 9
  • 19