I'm trying to write many custom XML and JSON providers, one for each domain class. However, only one custom class is working properly; the others are not being taken into account. For example, I have two classes in providers directory:
CustomDomainClass1Writer.groovy : tests for class DomainClass1 and collections of this class
CustomDomainClass2Writer.groovy : tests for class DomainClass2 and collections of this class
Only DomainClass1Writer is working, the XML and JSON output for the other class is the default one.
Am I missing something?
Thanks in advance and congrats for your plugin, it makes my life with Rest WS really easier!
OK, I've just figured myself. To use jackson -jaxrs-json provider, the following dependencies mus be added to BuildConfig.groovy:
runtime 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.0.5'
And then, the following bean registered in resources.groovy:
jacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider){
}
This is basic configuration, but it'll just do.
May I suggest to include jackson and jackson-jaxrs-provider in the plugin in order to take advantage of jackson library great performance?
Hi Martin:
Perhaps I misunderstood something here. I thought jax-rs Grails plugin did the JSON marshalling using Grails built-in Json serializer.
I was having problems with deep cloning and circular references, and needed more flexible control over my objects to serialize, so did some research and ended up using Jackson 2.0 with its providers. For what I've seen, jersey-json 1.12 still uses Jackson 1.X; Jackson 2.X is said to offer better performance and allows XML marshalling too.
Jackson 2.0 explicitly allows me using JSON serializing annotations in my domain classes, as well as custom serializers for any property. The truth is that I didn't know Jersey uses jackson; I guess Jersey using Jackson 2.0 version is a matter of time. What I don't completely get is the way serialization is made: if jax-rs plugin is using jersey, and jersey is using jackson, and serialization is accomplished via jackson providers and not Grails built-in providers, is it possible to annotate domain classes with Jackson annotations too?
Oh, dear, I should have figured it out. Thanks for sharing your experience, I think I'll test this part deploying directly on tomcat.
Probably you can adjust even more the excluded libraries of that configuration, but it seems it works showing no errors, so it just does fine for me.
So, Martin, wouldn't be nice including Jackson 2.X and swagger in grails jax-rs plugin? ;-)
El viernes, 2 de noviembre de 2012 15:09:19 UTC+1, Mike Wood escribió:Oh yes I have that problem! Seems like grails adds in a couple more levels of class loaders. Basically it comes down to they [Grails root class loaders] won't let classes inside jar files see those classes outside of jars. I've tried some workarounds like exploding the swagger jars - but never found a place I could put the class files to be included. Or putting my apps classes into a jar. I was also thinking about re-writing SwaggerContext.scala to also use the grailsApplication classloader which will find all classes - but I am not a scala person.
On the bright side when I build a war file and deploy to tomcat 6 it does work. So once outside of the the grails framework it functions. Hope that helps.
I'll switch to your config - much cleaner that what I am doing with jars in the lib dir. Appreciate you sending them!
Mike.