Friday, September 13, 2013

Grails 2.3 released - provides out of box support for REST

Lets face it, REST is the undisputed king of content integration. Almost every major product, framework, supports exposing its user-consumable services over REST. The burgeoning popularity of mobile apps and thin client technologies, is also driving the adoption of REST. Part of its success is the ease with which REST services can be consumed. All you need is an Http client, no matter if you are developing UIs in HTML5/javascript, java, PHP, Ruby, Python, Android, iOS, etc... all the platforms have mature Http client libraries that can consume REST services.

Grails 2.3 which was released a few days ago, now provides out of the box support for exposing grails services functionality over REST. Grails was always very good at providing an easy way to write CRUD services based on its GORM/Hibernate based server side. The only sore point was its proprietary server side presentation technology in the form of GSP, which did not decouple the UI controllers sufficiently from the views. You still had developers driving GSPs, and making frequent and radically changes to UI (almost as if it were a mashup), were difficult.

But now...Grails can be used to develop service layers of an application, very rapidly and same services can very easily be exposed as REST interfaces, which can be consumed by a variety of clients including mobile clients.

Heres how...Getting started, is so easy even a novice developer could do this.

You can start by downloading the latest Grails 2.3 release.
Go about creating a new grails app as usual
grails create-app sample1

Create a new domain class
grails create-domain-class contact

Now annotate the class as shown below in bold:

package sample1
import grails.rest.*

@Resource(uri='/contacts')
class Contact {
        static constraints = {  }
String firstName;
String mobile;
String email;
}


Thats it, now just run your grails app with 
grails run-app

Congratulations! You have just REST-ified your domain class Contact.

You can perform CRUD operations like create, read, update and delete on your domain class Contact using the following REST URLs.

To fetch list of all contacts from DB use  
Http GET to  http://localhost:8080/sample1/contacts.json

To fetch a particular Contact information given its id = 101, use
Http GET to  http://localhost:8080/sample1/contacts/101.json   (where 101 is the id of the Contact)

To add a new contact
Http POST to  http://localhost:8080/sample1/contacts.json
with request body being a json like {'firstName':'Ganesh Ghag','mobile':'7776661110','email':'none'}

To update an existing contact with id = 101
Http PUT to  http://localhost:8080/sample1/contacts/101
with request body being a json like {'firstName':'Ganesh Ghag Updated'}

To delete an existing contact with id = 101
Http DELETE  to http://localhost:8080/sample1/contacts/101


While invoking Http verbs like GET and POST is fairly simple from an HTML, to be able to invoke PUT and DELETE, we will use cURL as shown below.

The sample curl script below, creates a new conatct, updates it, deletes it, just like a good test script should.


rem inserting a new contact
curl -i -X POST -H "Content-Type: application/json" -d "{'firstName':'From CURL1','mobile':'7776661110','email':'none'}" http://localhost:8080/sample1/contacts.json

rem updating a contact
curl -i -X PUT -H "Content-Type: application/json" -d "{'firstName':'From CURL Update 1'}" http://localhost:8080/sample1/contacts/1

rem deleting a contact
curl -i -X DELETE http://localhost:8080/sample1/contacts/1

rem getting all contacts after use case execution
curl -i -H "Content-Type:application/json" http://localhost:8080/sample1/contacts.json



Kudos to the Grails team for giving the REST features out of the box.


Building REST-enabled CRUD applications has never been easier!

Cheers !