I am creating a Grails application that has read-only access to a web service that returns XML. Other than a username/password, the entirety of the data for this application will come from this external web service.
What is the best way to map variables in my domain classes to this data source? I have found many examples for custom ORM with a database, but none for XML. Where would I write the logic to make the HTTP call and extract the fields?
Any and all help is greatly appreciated.
We are very successfully using spring web services with MarkupBuilder/XmlSlurper. You can use the WebServiceTemplate to create a web service client to a given Uri, and send your request via an XML built with MarkupBuilder.
This has the big advantage for us to keep the WSDL as the central description of the interface, as a documentation artifact, no code generation and generated code to maintain, no annotations to add for jaxb bindings, no data objects.
We don’t use domain classes, we use the GPathResult obtained by slurping the XML result of the web service invocation directly, because in a duck-typing kind of way it is equivalent for the client to java objects. However, we don’t have a web interface. If you have a web interface, I would create Command Objects with a
method, or maybe
would work, depending on your wsdl’s XSD schema. this way you take advantage of grails data binding and validation.
For the view’s data models, maybe passing a duck-type of the command object (compatible XML object) is enough, so you don’t have to marshall both ways.
Another caveat is that the current version of the grails spring-ws plugin is out of date. I don’t event think it will play well with grails 2.0 because of old spring-security-core classes in ther. The good news is that if you’re only building a client, you hardly need the plugin at all. Just include the most recent spring web services jar and use the WebServiceTemplate.sendToEndpoint method.