Search code examples
javajsonrestserializationosgi

Serialize POJO to JSON through a provider


What is a common way to serialize Java Beans to JSON in a automatic fashion? The @Produces(MediaType.APPLICATION_JSON) doesnt seem to handle the conversion under the hood. Do we create our own JSON Providers or is there another way? Currently im trying to handle it via the ContextResolver:

https://docs.oracle.com/javaee/7/api/javax/ws/rs/ext/ContextResolver.html

It is my understanding that the @Produces annotation should help create the expected output if a java object is returned from one of the resource class methods.


Solution

  • You do not have to code the jackson based provider yourself.

    In apache karaf you can simply install the feature aries-jax-rs-whiteboard-jackson. If you are not using karaf you can install the bundles from the feature yourself:

    https://github.com/apache/aries-jax-rs-whiteboard/blob/master/jax-rs.features/src/main/feature/feature.xml