Chances are that if you use JAX-RS in WebSphere Liberty you use JSON to format your data. If so, it is equally likely that you are using Jackson to do it. Liberty’s JAX-RS implementation actually uses Jackson as its default JSON provider. That’s pretty cool, you say, but how can I take advantage of that in my application?

Good question! Liberty uses Jackson but intentionally does not expose it to user applications. So if you want to use some of the cool features in Jackson, like annotating fields to ignore or providing serialization processing instructions, you just can’t do it… or can you?

Sure you can, just bring your own Jackson! In other words, package the Jackson JAX-RS provider JAR files in your application (in your WAR file). The JARs you’ll need are:

The Jackson JAX-RS JSON provider class is automatically registered if all of the JAR files are in the WAR’s WEB-INF/lib directory. However, if you specify any classes in your Application subclass using the getClasses() or getSingletons() methods, you need to register the provider class, like so:

public class HelloWorldApplication extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<>();
        // ...
        classes.add(JacksonJaxbJsonProvider.class);
        return classes;
    }

}

Automatic discovery and registration is not available in the JAX-RS Client APIs, so you would need to explicitly register the provider class, like so:

Client client = ClientBuilder.newClient().register(JacksonJsonProvider.class);

That’s it! Now Liberty’s JAX-RS implementation will use the Jackson provider from your application instead of the one built-in to Liberty. This means that your application can now use those cool Jackson features.

Do I need to do any classloading tricks? Like parentLast? Nope, because the Liberty server does not expose the Jackson API packages, your application can load only the Jackson classes that you provide. It also means that the application works regardless of the delegation policy–so you can use parentLast if you really want to.

Do you have a sample application that might help me understand how this works? You bet. Check out the sample.BYOJackson app at the WASdev GitHub repo!

Thanks!

Join The Discussion

Your email address will not be published. Required fields are marked *