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

1k views Asked by At

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 ?

2

There are 2 answers

0
MKB On BEST ANSWER

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']
2
billjamesdev On

Alternatively, if you follow the method naming convention for REST controllers... you could get away with:

"/product/$id" (resources:'product')

Here's some good info: http://mrhaki.blogspot.com/2013/11/grails-goodness-customize-resource.html