Grails format oriented response

Posted By : Ashish Sharma | 30-Jan-2014

Grails format Oriented Response

In Grails we can get the response in the format we want i.e. JSON or XML, based on our request

To get the format oriented response:

  1. In URL-Mapping define the proper URL for your action
    	"/myUrl/$param(.${format})"(controller:"myController", action:"myAction", parseRequest:true)
    	
  2. Now add data to a HashMap variable in your action to send it in a required format as response
    	HashMap res = new HashMap();
    	res.result = "success";
    	res.message = "Any message";
    	res.data = getListDataFromMethod();
    	respond res, [formats:['json', 'xml']];
    	
  3. To get above response call this URL from ajax or hit it directly to browser
    	http://localhost:8080/myGrailsApp/myUrl/3.json
    	

 

About Author

Author Image
Ashish Sharma

Ashish is a bright Groovy and Grails developer and have worked on development of various SaaS applications using Grails technologies. Ashish likes PC games and works out at Gym in his free time.

Request for Proposal

Name is required

Comment is required

Sending message..