automation-1 automation-2 automation-3 automation-4 consultancy-1 consultancy-2 consultancy-3 consultancy-4 facebook google-plus outsourcing-1 outsourcing-2 outsourcing-3 outsourcing-4 power-engineering-1 power-engineering-2 power-engineering-3 power-engineering-4 twitter

How to register Genson with Jersey

Genson and Jersey overview

Jersey is a great library for developing RESTful web services. It is possible to use many different media types for representing data; if some media type is missing, additional libraries can be registered.

Genson is a JSON <-> Java library, very fast with small footprint.

It is possible to use Genson within Jersey as a JSON serializer/deserializer. Registration of GENSON library with Jersey is different depending on the Jersey version:

Jersey 2.9 and below:

It is enough to have Genson in classpath, Jersey will atomatically find it and use. No special configuration is needed.

Jersey 2.10 and above:

Automatic registration is disabled in newer version of the libraries (I have no idea why). It is required to register Jersey using Register method of the ResourceConfig class. Below is two step example how to do it:

  • Create class that extends ResourceConfig
package rs.iten.server;

import org.glassfish.jersey.server.ResourceConfig;

import com.owlike.genson.ext.jaxrs.GensonJsonConverter;

public class ServerApplication extends ResourceConfig {
    public ServerApplication() {
        super();
        register(GensonJsonConverter.class);
    }
}
  • Set new class as Application parameter in web.xml configuration file:
<init-param>
    <param-name>javax.ws.rs.Application</param-name>
    <param-value>rs.iten.server.ServerApplication</param-value>
</init-param>

Share this article additional message.