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.

Whyvra FVR
  • 73
  • 1
  • 8
  • How do you use Jackson 2 with Payara 4? If you enable Jackson 2 in Payara 5 it should automatically disable the default Jsonb provider. See this answer: https://stackoverflow.com/a/18318314/784594 – OndroMih Apr 14 '18 at 09:40
  • Well, I've just added Jackson's 2.8.9 dependencies in my pom.xml and disabled the default moxyJson provider in my web.xml. – Whyvra FVR Apr 20 '18 at 08:01
  • What do you get if you do the same with Payara 5? I think that adding Jackson disables the JSON-B provider automatically. – OndroMih Apr 20 '18 at 12:59
  • Nothing. It uses JSON-B (Yasson) and I actually can't find a way to bypass it. – Whyvra FVR Apr 22 '18 at 16:29
  • Is it the expected behavior or there is an improvement? – Whyvra FVR May 02 '18 at 08:42
  • Looking at the source code, Jersey won't register another feature if there's one already registered. I thought that a feature provided by your app would take precedence but it's not the case. Moxy provides a special property to disable it but JsonB feature doesn't. Still, it's possible to prefer a specific feature by setting `jersey.config.jsonFeature` property to the specific feature, which is `JacksonFeature` for Jackson. I'll add a detailed answer – OndroMih May 03 '18 at 16:31

2 Answers2

7

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.

Tobias
  • 43
  • 4
OndroMih
  • 7,280
  • 1
  • 26
  • 44
  • Hi Ondrej. Does it mean I need to add "org.glassfish.jersey" dependencies as well? JacksonFeature is not located in com.fasterxml.jackson.jaxrs or whatever jackson provider you may have. Do you have a web.xml example? – Whyvra FVR Jun 28 '18 at 15:20
  • Well, you will feel happy to know it works... but I'm surprised. I didn't add the mentioned dependencies and it works. You will find hereunder an extract from my `web.xml` – Whyvra FVR Jun 28 '18 at 15:26
  • ` jersey.config.server.provider.classnames org.glassfish.jersey.jackson.JacksonFeature ` – Whyvra FVR Jun 28 '18 at 15:28
  • ` jersey.config.server.provider.packages fr.trendev.comptandye.services, com.fasterxml.jackson.jaxrs ` – Whyvra FVR Jun 28 '18 at 15:29
  • I don't understand why but `org.glassfish.jersey.jackson.JacksonFeature` is not imported and it works. Does it mean that the parser will just analyze the property... without checking if the file/class is present? **Amazing and strange hack ;)** – Whyvra FVR Jun 28 '18 at 15:31
  • This was such a pain I had `getClasses` overriden and lost the auto-discover. Where does someone find these configuration at? I was all over payar's site – nerdlyist Jun 29 '18 at 00:45
  • You shouldn't need to set the `jersey.config.server.provider.packages` parameter. This parameter overrides the `getClasses` method and the autodiscovery mechanism. Also, instead of `jersey.config.server.provider.classnames` init-param, you can use `jersey.config.jsonFeature` context-param - see an example in my updated reply – OndroMih Jul 02 '18 at 14:40
  • Since your app used Jackson on Payara 4, it already contains all required Jackson dependencies, including `org.glassfish.jersey.jackson.JacksonFeature` - check your application's WEB-INF/classes folder in the WAR file. – OndroMih Jul 02 '18 at 14:42
  • Hi @OndrejM, I've finally just overridden the getProperties method and removed my messy web.xml file and it works. Thanks a lot for your support and your explanation ;) – Whyvra FVR Oct 15 '18 at 18:01
  • 1
    Hi @JulienSié, I'm happy that you finally made your app work! You can now mark this answer as the correct answer ;-) – OndroMih Oct 16 '18 at 08:32
2

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>
Tobias
  • 43
  • 4