With Jersey, I am currently using the Gson library to convert my pojo to json back and forth.
However, I’ve discovered Jackson to be a lot faster so we are switching to this.
I already have a Gson custom Provider and Reader, and will now create (a modified) version for Jackson.
But during development, it would be good if I could swap them but is this possible? Since I mark the json provider with @produces and @consumes I dont think Jersey could resolve which one to use if there were two providers, each with the above annotations for the same type, in this case APPLICATION_JSON.
Is it possible to specify multiple Json providers, and then swap them in some config file (web.xml??) ?
I know if I created a Json Wrapper object I could code to an interface and using spring to swap implementations easily (jackson imp vs gson imp) but this would mean i wouldnt be doing it the Jax RS way. Also, I would be forced to return a String for all the GETs rather than let the providers convert it for me implicitly.
Any thoughts?
thanks
If you create your own subclass of ResourceConfig (or JAX-RS Application), it has a method named getClasses() – that’s where you have to return set of resource and provider classes that your application uses. Now, seems you are using some of the pre-packaged ResourceConfig – Jersey has ResourceConfig that does class-path scanning (i.e. if you configure that one – or none – since this one is the default) it scans the whole class path for resource classes). There is one that is capable of package scanning – that one scans just the packages you list in your init params of Jersey servlet, etc.
Actually, the package scanning one may be best for you. Just make sure you put each of your JSON providers in a different package and then configure in web.xml which packages should be scanned for resources and providers. Here is an example of web.xml that configures Jersey to look for resources and providers in com.mycompany.resource and com.mycompany.providers.gson packages: