In JAX-RS 2.1, the internal library used for serialization and deserialization between JSON and Java objects has changed from Jackson to Yasson. Yasson is the reference implementation of JSON-B. Jackson and Yasson will serialize time-related objects in different ways, for example:
| Jackson | Yasson | |
|---|---|---|
| java.util.Date | 726213720000 | 1993-02-05T06:02:00Z[UTC] |
| java.util.Calendar | 726213720000 | 1993-02-05T00:00:00-06:00[America/Chicago] |
Any JAX-RS resource methods which produce or consume JSON-type data may be effected. This rule flags any java.util.Date or java.util.Calendar fields that are contained in an object produced or consumed by a JAX-RS
resource method.
The following is an example of JAX-RS resource methods which produce and consume a Java Object containing a java.util.Date and java.util.Calendar field:
|
@Path("/") @ApplicationScoped public class MyService { @GET @Path("/get") @Produces(MediaType.APPLICATION_JSON) public DataObject getData() { //... } @GET @Path("/set") @Consumes(MediaType.APPLICATION_JSON) public void setData(DataObject data) { //... } } |
|
import java.util.Calendar; import java.util.Date; public class DataObject { public Date juDate; public Calendar juCalendar; } |
The rule will flag the java.util.Date and java.util.Calendar fields in the DataObject object. In the source scanner, a quick fix is available for this rule. The quick fix adds an
@JsonbDateFormat(value = JsonbDateFormat.TIME_IN_MILLIS) to the fields which converts the output to epoch millis. After the quick fix has run, the DataObject class will change to:
|
import java.util.Calendar; import java.util.Date; import javax.json.bind.annotation.JsonbDateFormat; public class DataObject { @JsonbDateFormat(value = JsonbDateFormat.TIME_IN_MILLIS) public Date juDate; @JsonbDateFormat(value = JsonbDateFormat.TIME_IN_MILLIS) public Calendar juCalendar; } |
Note: In order to access the @javax.json.bind.annotation.JsonbDateFormat
annotation added by the quick fix, you will need to add the jsonb feature to your Liberty
server.xml configuration file. For example, add the jsonb-1.0 feature if you are using the
jaxrs-2.1 feature.
For more details about behavior differences between Jackson and Yasson, see the Is it time for a JSON binding standard? article comparing Jackson and JSON-B behavior.