Search code examples
restgrailsurl-mappinggrails-3.0

REST resources defined in UrlMappings results in 404


Using grails 3.0.9, I've defined 2 Domains:

Game -> hasMany plays
Play -> belongsTo game

I've added the following to my UrlMappings.groovy file.

"/games"(resources: "game") {
  "/plays"(resources: "play")
}

Using Postman, I'm doing a get on http://localhost:8080/games and I get a 404.

If I remove the UrlMappings and place an @Resource(uri='/games') on the Game domain, it returns a 200, as expected. Here is what I get when I run url-mappings-report:

Dynamic Mappings
 |    *     | ERROR: 404                                        | View:   /notFound        |
 |    *     | ERROR: 500                                        | View:   /error           |
 |    *     | /                                                 | View:   /index           |
 |    *     | /${controller}/${action}?/${id}?(.${format)?      | Action: (default action) |

Controller: game
 |   GET    | /games/create                                     | Action: create           |
 |   GET    | /games/${id}/edit                                 | Action: edit             |
 |   POST   | /games                                            | Action: save             |
 |   GET    | /games                                            | Action: index            |
 |  DELETE  | /games/${id}                                      | Action: delete           |
 |  PATCH   | /games/${id}                                      | Action: patch            |
 |   PUT    | /games/${id}                                      | Action: update           |
 |   GET    | /games/${id}                                      | Action: show             |

Controller: play
 |   GET    | /games/${gameId}/plays/create                     | Action: create           |
 |   GET    | /games/${gameId}/plays/${id}/edit                 | Action: edit             |
 |   POST   | /games/${gameId}/plays                            | Action: save             |
 |   GET    | /games/${gameId}/plays                            | Action: index            |
 |  DELETE  | /games/${gameId}/plays/${id}                      | Action: delete           |
 |  PATCH   | /games/${gameId}/plays/${id}                      | Action: patch            |
 |   PUT    | /games/${gameId}/plays/${id}                      | Action: update           |
 |   GET    | /games/${gameId}/plays/${id}                      | Action: show             |

Not sure why the UrlMappings are not working.


Solution

  • My understanding is the UrlMappings syntax "/games"(resources: "game") automatically generates the associated RESTful mappings (and only the mappings), while the use of the @Resource(uri='/games') syntax generates the controller/actions.

    With the "/games"(resources: "game"), you still need to define your own controller. The way you've written this question, I get the feeling that you haven't done this.