jsonjax-rswildflyjsonbyasson

Date serialization ignores ContextResolver in REST request


I have an application based on Wildfly 15, which uses Yasson to serialize entities in REST requests. I use the javaee-api 8.0.1 and created a ContextResolver for configuring the date serialization format like in https://stackoverflow.com/a/56300246/584532.

@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JsonbDateConfig implements ContextResolver<Jsonb> {
  // ...
}

However, when sending REST requests using the following code, the configuration is ignored (no debugging breakpoint in the methods of JsonbDateConfig is triggered).

Response response = target.path(REST_SERVICE_NAME)
  .request()
  .post(Entity.entity(dataTO, MediaType.APPLICATION_JSON));

Consequently, the resulting JSON contains an inproperly formatted date value.

I could create a JsonbAdapter and add the annotation @JsonbTypeAdapter(DateAdapter.class) to the field of type java.util.Date. However, I prefer a solution that applies to all date fields. What is the solution with the ContextResolver not working?

Note that Wildfly loads my implementation class of ContextResolver during startup (class loading breakpoint) and this resolver is used when I receive incoming REST requests.


Solution

  • Since you're using a JAX-RS client you need to register the provider with the client.

    Response response = target.path(REST_SERVICE_NAME)
      .register(JsonbDateConfig.class)
      .request()
      .post(Entity.entity(dataTO, MediaType.APPLICATION_JSON));