Grails - Soap

Since working with, or creating WebServices is something webdevelopers will probably do regularly (or stumble upon in the very near future) I decided to have a look at Grails' support for SOAP.

First thing I wanted to see was how to expose a Grails service via Soap. Due to the excellent documentation at the Grails website this proved to be really simple:

Setup
  1. download the XFire plugin here
  2. install the plugin by running "grails install-plugin /path/to/grails-XFire-x.x.x.zip" from the root of your project
Create a service
  1. run "grails create-service Task" from the root of your project
  2. Implement it and configure it for XFire remoting:

    GROOVY:
    1. class TaskService {
    2.     static expose=['xfire'] // expose remotely using xfire
    3.    
    4.     boolean transactional = true
    5.    
    6.     def String[] findTasksBySubject(String s) {
    7.         def c = Task.createCriteria()
    8.         def results = c{
    9.             like("subject", "%${s}%")
    10.         }
    11.  
    12.         return results*.subject
    13.     }
    14. }

    • Notice the *. (spread-dot) operator? It collects results from call the consequent method on each item in the list, cool!
    • Notice the Hibernate Criteria Builder, more info here
    • Notice the 'transactional' propery, setting it to true results in all methods of a service are wrapped in a transaction.
  3. Start your application, and point your browser to: http://localhost:8080/ptodo/services/task?wsdl
    which gives you the WSDL describing your service!
  4. done!

Of course there is no point in having a WebService without a client... which is only 3 lines of Groovy away. From your favorite Groovy shell, run (after installing the SOAP libs):

GROOVY:
  1. groovy> import groovy.net.soap.SoapClient
  2. groovy> def proxy = new SoapClient("http://localhost:8080/ptodo/services/task?wsdl")
  3. groovy> proxy.findTasksBySubject("XFire")
  4. groovy> go
  5.  
  6. ===> [Check the XFire plugin]

Almost instant gratification!


0 Responses to “Grails - Soap”

  1. No Comments

Leave a Reply





About

Welcome to the weblog of Peter Maas. Here you'll find various posts related to stuff I like (like my kids and espresso) and stuff I do (like developing software).

JavaOne 2008 Pictures


nearby hotel Stretched Limo Okke en Rudie Charles Nutter & Guillaume Laforge javaone2008 keynote Scribbled Sun Logo Greenland Hotel room alcatraz smashmouth Rudie javaone 2008 goodybag golden_gate_warning_sign Stage being build in the nearby park Java + You on a cab Acme Anvile at CommunityOne Keynote Joshua Bloch at JavaOne2008 Golden Gate sea_lion Moscone Center - JavaOne
View more photos >

Categories



Meld u aan voor PayPal en begin direct met het accepteren van creditcardbetalingen.