3

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.

Steven
  • 33
  • 3
  • Welcome @Stackoverflow at this point - If you use the search from stackoverflow you also can find answers to this question easility without asking it again. I will have to mark your question as duplicate so other can find the up2date question+answer. – DominikAngerer Apr 19 '15 at 15:32
  • possible duplicate of [Using Gson instead of Jackson in Jersey](http://stackoverflow.com/questions/9516224/using-gson-instead-of-jackson-in-jersey) – DominikAngerer Apr 19 '15 at 15:33

1 Answers1

3

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.

Community
  • 1
  • 1
DominikAngerer
  • 6,354
  • 5
  • 33
  • 60