I started my study about grails few weeks ago, grails is a rad framework, very similar to Ruby on Rails but with a lot of nice features, one of them is the builtin support for rest webservice, you just need create your domain and controller classes, start the application and then you can submit your xml over GET, POST, PUT and DELETE methods! Very easy and handy!
You only need type these command on command line window:
grails create-app restservice
grails create-domain-class Person
grails create-controller Person
Now you must edit Person.groovy at restservice/grails-app/domain folder and then add Person class properties like below:
Save the file, now edit the file PersonController.groovy at restservice/grails-app/controller/PersonController.grooy and put the code below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import grails.converters.* class PersonController { def show = { if(params.id && Person.exists(params.id)) { def person = Person.findByName(params.id) render person as XML } else { def allPersons = Person.list() render allPersons as XML } } def save = { def person = new Person(params['person']) if(person.save()) { render person as XML } else { def errors = person.errors .allErrors.collect { g.message(error:it) } render(contentType:"text/xml") { error { for(err in errors) { message(error:err) } } } } } def delete = { ... } def update = { ... } } |
You must add the mapping to the methods on this controller too in UrlMappings.groovy at restservices/conf directory:
1 2 3 4 5 6 | static mappings = { "/person/$id?"(controller:"person"){ action = [GET:"show", PUT:"update", DELETE:"delete", POST:"save"] } } |
Finally, you must start the application by running the following command:
grails run-app
The webservice can be easy tested by submitting xml data to url http://localhost:8080/restservice/person via GET, POST, DELETE and PUT http methods, GET can be used to retrieve person data in xml format, POST to submit data in xml format to be stored on database, DELETE to remove person data from database and PUT to update person data.
As you can see, rest webservices are much more simple than SOAP webservices and with grails you can create a rest webservices in less than 10 minutes!
Grails is a very exciting framework and I hope to post more content about it soon!
Wow! Another great post Rogerio
Grails is looks to me very interesting! At home i will go to do some tests with it!!