birnbaum birnbaum - 6 months ago 81
Java Question

How is javax.ws.rs.client.Entity serializing objects to json?

What serializer is

using to serialize/deserialize objects? Is it somehow possible to use a custom serializer?

In my case the serialization is wrong because my object contains fields with the Guava Optional data type and absent values are returned as
{"present":false}
instead of
null
.

Answer

The JSON serializer isn't specified by JAX-RS, it depends on your configuration. For example, Jersey JAX-RS allows several (https://jersey.java.net/documentation/latest/media.html), including

  • MOXy
  • Java API for JSON Processing (JSON-P)
  • Jackson
  • Jettison

But a better solution is not to use Optional (either Guava or Java 8) for fields. See http://blog.joda.org/2014/11/optional-in-java-se-8.html

My only fear is that Optional will be overused. Please focus on using it as a return type (from methods that perform some useful piece of functionality) Please don't use it as the field of a Java-Bean.

Comments