Enterprise, RIA/Web 2.0, and I.T. Management Solutions

Home / Blog / Featured / J2EE & Grails / Quick Tips / A Grails, RESTful Services Update

Greetings Grail’ngs,

I have been SO, SO, SO impressed with Grails since I started learning it less then two weeks ago.  It’s almost as easy to learn and implement as PHP.  Of course, it lends itself well to the Java/J2EE paradigm since it is a subset of the Java language–just in the form of a scripting language–but in so doing it still brings along some of the complexity inherent in moving a J2EE project.

At any rate, I wanted to cut to the chase about the main thrust of this post:  Grails and RESTful services through content negotiation.

From the start of using Grails, I wanted to expose my service layers as a group of Grails RESTful services so that various types of clients–Flex/Flash, PHP, JSP, and the like–can get what they need in a uniform manner.  So, I found this article here which gave me a good start.  However, it was outdated insofar as content negotiation and the scaffolding goes.  I’ve attached an updated Controller.groovy file (that, after you do “grails install-templates”, comes under src/templates/scaffolding/).  It’s not exactly like what Tomas has written as I had to do some debugging/updating of his code to get it to work with Grails 1.0.x and later.

Let me know if it works for you!

Controller.groovy
*My ISP is having perm problems so I’ve cut/pasted Controller.groovy below:


/**********BEGIN CODE*************/
import grails.converters.*

<%=packageName ? “package ${packageName}\n\n” : ”%>class ${className}Controller {

def index = { redirect(action:list,params:params) }

// the delete, save and update actions only accept POST requests
// static allowedMethods = [delete:'POST', save:'POST', update:'POST']
def list = {
params.max = Math.min( params.max ? params.max.toInteger() : 10, 100)
withFormat{
html{ [ ${propertyName}List: ${className}.list( params ), ${propertyName}Total: ${className}.count() ] }
xml{ render ${className}.list( params ) as XML }
}
}

def show = {
withFormat{
html{
def ${propertyName} = ${className}.get( params.id )
if(!${propertyName}) {
flash.message = “${className} not found with id \${params.id}”
redirect(action:list)
}
else { return [ ${propertyName} : ${propertyName} ] }
}
xml{
if(params.id && ${className}.get(params.id)) {
def c = ${className}.get( params.id )
render c as XML
}
else {
def all = ${className}.list( params )
render all as XML
}
}
}
}

def delete = {
def ${propertyName} = ${className}.get( params.id )
if(${propertyName}) {
try {
${propertyName}.delete()
flash.message = “${className} \${params.id} deleted”
redirect(action:list)
}
catch(org.springframework.dao.DataIntegrityViolationException e) {
flash.message = “${className} \${params.id} could not be deleted”
redirect(action:show,id:params.id)
}
}
else {
flash.message = “${className} not found with id \${params.id}”
redirect(action:list)
}
}

def edit = {
def ${propertyName} = ${className}.get( params.id )

if(!${propertyName}) {
flash.message = “${className} not found with id \${params.id}”
redirect(action:list)
}
else {
return [ ${propertyName} : ${propertyName} ]
}
}

def update = {
def ${propertyName} = ${className}.get( params.id )
if(${propertyName}) {
if(params.version) {
def version = params.version.toLong()
if(${propertyName}.version > version) {
<%def lowerCaseName = grails.util.GrailsNameUtils.getPropertyName(className)%>
${propertyName}.errors.rejectValue(”version”, “${lowerCaseName}.optimistic.locking.failure”, “Another user has updated this ${className} while you were editing.”)
render(view:’edit’,model:[${propertyName}:${propertyName}])
return
}
}
${propertyName}.properties = params
if(!${propertyName}.hasErrors() && ${propertyName}.save()) {
flash.message = “${className} \${params.id} updated”
redirect(action:show,id:${propertyName}.id)
}
else {
render(view:’edit’,model:[${propertyName}:${propertyName}])
}
}
else {
flash.message = “${className} not found with id \${params.id}”
redirect(action:edit,id:params.id)
}
}

def create = {
def ${propertyName} = new ${className}()
${propertyName}.properties = params
return ['${propertyName}':${propertyName}]
}

def save = {
def ${propertyName} = new ${className}(params)
if(!${propertyName}.hasErrors() && ${propertyName}.save()) {
flash.message = “${className} \${${propertyName}.id} created”
redirect(action:show,id:${propertyName}.id)
}
else {
render(view:’create’,model:[${propertyName}:${propertyName}])
}
}
}
/*********END CODE**********/

NOTE: Please keep in mind there are no guarantees with this software.  Use at your own risk!

Share and Enjoy:
  • Facebook
  • FriendFeed
  • Twitter
  • LinkedIn
  • Suggest to Techmeme via Twitter
  • Google Bookmarks
  • Reddit
  • Digg
  • del.icio.us
  • Slashdot
  • DZone
  • StumbleUpon
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • Live
  • MSN Reporter
  • NewsVine
  • Simpy
  • Blogosphere News
  • Sphinn
  • Linkter
  • Mixx
  • BlinkList
  • blogmarks
  • BlogMemes Fr
  • RSS
  • Print this article!
  • E-mail this story to a friend!

Leave a Reply