Using Gson instead of Jackson in Jersey

Moshe Bixenshpaner picture Moshe Bixenshpaner · Mar 1, 2012 · Viewed 28.2k times · Source

I have a RESTful service that consumes and produces JSON objects, and I would like Jersey to use Gson instead of Jackson.

How can this be done...?

Answer

Philipp Reichart picture Philipp Reichart · Mar 1, 2012

You need to write custom implementations of MessageBodyReader and MessageBodyWriter (possibly in the same class) and register with Jersey (if you use package scanning, the @Provider annotation is enough) -- pretty much like JacksonJsonProvider does it:

@Provider
@Consumes({MediaType.APPLICATION_JSON, "text/json"})
@Produces({MediaType.APPLICATION_JSON, "text/json"})
class GsonJsonProvider implements
    MessageBodyReader<Object>,
    MessageBodyWriter<Object> { ...