Search code examples
grailsurl-mappinghttp-methodcontroller-action

Grails: Same URL Mapping to different actions per different HTTP methods


I'm using Grails v3.2.9

In official documentation I found the following for mapping to http methods:

static mappings = {
   "/product/$id"(controller:"product", action: "update", method: "PUT")
}

But this is not enough. What I need is to have one mapping which maps to different actions(in the same controller) based on HTTP method.

Any idea ?


Solution

  • Add URLMappings like --

    "/product/api/v2/book" (controller: 'book') {
        action = [GET: 'show', POST: 'update']
    }
    

    Also, it is good to add method constraint in controller --

      static allowedMethods = [show: 'GET', update: 'POST']