Skip to content

Resource Config Generation

Ian Ibbotson edited this page Jun 20, 2017 · 3 revisions

REST resources are a grails idiom whereby domain classes can be exposed directly as url endpoints with a default set of actions. In order to expose a domain class as a grails resource the @Resource annotation from grails.rest.Resource is used:: for example

package web.toolkit.starter

import grails.rest.*

@Resource(uri='/widget')
class Widget {

  String widgetName

 static constraints = { 
  } 
}

RESTful Resource config for each Domain class that responds restfully is published at: /config Viewing that URL you should see JSON similar to the below returned:

{
  "resources": {
    "Author": {
      "baseUri": "/author"
    },
    "Book": {
      "baseUri": "/book"
    }
  }
}

Visiting /config/extended adds extra controller and action details like the below:

{
  "resources": {
    "Author": {
      "mapping": {
        "index": [
          {
            "method": "GET",
            "uri": "/author",
            "type": "index"
          }
        ],
        "save": [
          {
            "method": "POST",
            "uri": "/author",
            "type": "save"
          }
        ],
        "show": [
          {
            "method": "GET",
            "uri": "/author/{id}",
            "type": "show"
          }
        ],
        "update": [
          {
            "method": "PUT",
            "uri": "/author/{id}",
            "type": "update"
          }
        ],
        "delete": [
          {
            "method": "DELETE",
            "uri": "/author/{id}",
            "type": "delete"
          }
        ],
        "patch": [
          {
            "method": "PATCH",
            "uri": "/author/{id}",
            "type": "patch"
          }
        ]
      },
      "baseUri": "/author"
    },
    "Book": {
      "mapping": {
        "index": [
          {
            "method": "GET",
            "uri": "/book",
            "type": "index"
          }
        ],
        "save": [
          {
            "method": "POST",
            "uri": "/book",
            "type": "save"
          }
        ],
        "show": [
          {
            "method": "GET",
            "uri": "/book/{id}",
            "type": "show"
          }
        ],
        "update": [
          {
            "method": "PUT",
            "uri": "/book/{id}",
            "type": "update"
          }
        ],
        "delete": [
          {
            "method": "DELETE",
            "uri": "/book/{id}",
            "type": "delete"
          }
        ],
        "patch": [
          {
            "method": "PATCH",
            "uri": "/book/{id}",
            "type": "patch"
          }
        ]
      },
      "baseUri": "/book"
    }
  }
}

Clone this wiki locally