Using scaffolding with REST in grails

You are developing an app and using Grails as its backend, now days REST is the new buzzword and you’ll probably want to create a REST api via Grails. Fortunately, Grails support REST api out of the box (version 2.3 and above), but what happens if you want to support scaffolding + REST api in the same controller.

Well, it seems that Grails supports it, although not in the most elegant way, by using content negotiation.
You have an ability to tell Grails in what format you want your response either by supplying a suffix to the request (.json) or by specifying it in the header. Either way it gives you the following ability:

  1. Define a regular Grails controller.
    class FamilyController {
     static scaffold = true
    }
    
  2. When calling the controller you can add a suffix to you call
    http://localhost:8080/app/family/index.json
  3. You should be able to get back results as json, same applies to xml.
  4. As you can see the default content being served is html so you can also access the scaffolding page.

The annoying part here is the fact that you cannot access your resource in a “by the book” manner, .e.g. you need to use an action like show, edit/${id} to access your resource and you can not do it directly via family/1

Advertisements

Leave a comment

Filed under Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s