Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gson used as Provider using Jersey 1.x

Tags:

java

gson

I'm currently looking for a simply way to use Gson instead of Jackson as Provider. Currently I create the Json manually with Gson like this:

Gson gson = new Gson();
String s = gson.toJson(object);

and return it as Response with the specific status message - but that can't be the best solution also with Jackson I simply would can add the object itself.

Looking for a good example which works also for Jersey 1.x - because I want to move also some old project's to it.

I already tried this: http://eclipsesource.com/blogs/2012/11/02/integrating-gson-into-a-jax-rs-based-application/ but couldn't get it to work.

Any help appreciated.

like image 508
Steven Avatar asked Nov 23 '25 04:11

Steven


1 Answers

I created this running example on Github https://github.com/DominikAngerer/java-GsonJerseyProvider

It has an improved implementation of the GsonJerseyProvider you found yourself - but also with the web.xml configuration part, because you also need to tell jersey to use your provider.

<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
    com.dominikangerer.gson.provider.v1.util,
    com.dominikangerer.gson.provider.v1.controller
</param-value>

is here the key part - it will scan the controller package and also the util package where the provider was added.

You can also find an answer from me to this here: https://stackoverflow.com/a/26829468/1581725

This provider will work for Jersey 1.x and also for Jersey 2.x.

like image 115
DominikAngerer Avatar answered Nov 24 '25 16:11

DominikAngerer