Search code examples
jax-rsjsonbjackson2payarapayara-micro

How to use Jackson 2 in Payara 5?


I'm using Jackson 2 with Payara 4 and I would liked to use Jackson 2 in Payara 5.

Using JAX-RS, I also would like to avoid changing annotations and so on...

In Payara 5 the default Jsonb provider is Yasson. Any ideas to disable it and use Jackson instead? All comments/ideas are welcome :-)

NB: Yasson is very interesting but handle abstract class or interface serialization/deserialization is a little more complex than putting a Jackson annotation. My current understanding is that it requires to implement a JsonbSerializer/Deserializer but actually the serializer/deserializer is only available on field/method (an issue is opened for class, which will be very helpful). Anyway, migrating to Yasson will mean implementing many serializer/deserializer as required (for entities and of course collections) but I guess it's a hard stuff.


Solution

  • You need to set the property jersey.config.jsonFeature to JacksonFeature so that the default JsonB feature isn't registered.

    You can set it either in the code by overriding the Application.getProperties() method, or set the property in web.xml as context-param:

    <context-param>
      <param-name>jersey.config.jsonFeature</param-name>
      <param-value>JacksonFeature</param-value>
    </context-param>
    

    If your Jersey Servlet is explicitly declared at web.xml (either as javax.ws.rs.core.Application or a as custom subclass), then use init-param instead, within the Jersey Application servlet declaration:

    <servlet>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <init-param>
            <param-name>jersey.config.jsonFeature</param-name>
            <param-value>JacksonFeature</param-value>
        </init-param>
        ...
    </servlet>
    

    You also need to add Jackson dependencies into your application - but you have probably done that already so ignore this.

    Explanation:

    The MOXy feature provides a property jersey.config.disableMoxyJson to disable it. The JsonB feature default in Payara 5 doesn't provide such property but will not register itself if jersey.config.jsonFeature property exists and is not JsonBindingFeature. The same property works for all Jersey features so setting it to JacksonFeature will allow only the JacksonFeature to be registered.