5

I have the following:

"/api/users"(controller: "user") {
   action = [GET:"list"]
}

Doing a call to http://localhost:8080/platform/users I get a list of users back. Then I added this:

"/api/users"(controller: "user") {
   action = [POST:"save"]
}

And now I get a 404 and it is not hitting either method in UserController. I'd like to be able to use the same URL with the verb controlling which action. Am I doing this wrong or does Grails not support this?

Gregg
  • 34,973
  • 19
  • 109
  • 214

2 Answers2

11

From the Grails docs: URL Mappings

static mappings = {
   "/product/$id"(controller:"product") {
       action = [GET:"show", PUT:"update", DELETE:"delete", POST:"save"]
   }
}

For your case:

"/api/users"(controller: "user") {
   action = [GET:"list",POST:"save"]
}
James Kleeh
  • 12,094
  • 5
  • 34
  • 61
  • I feel like an idiot. I knew that. Must..get..sleep. Thanks James. – Gregg Dec 22 '12 at 06:03
  • What about if the methods are in separate controllers? Or for example /users goes to one action and /users/1111 goes to another? – Jackie Sep 16 '14 at 13:42
  • @Jackie than probably you have bad planned app structure. Remember also about redirects if it's more sophisticated. – Michal_Szulc Dec 21 '15 at 07:34
1

Check your userController to see if there is allowedMethods defined accordingly like this:

class UserController {

    static allowedMethods = [save: "POST", list: "GET"]

    def list() {
    .....
    }

    def save() {
    .....
    }
}
coderLMN
  • 3,076
  • 1
  • 21
  • 26