使用grails 3.0.9,我定义了2个域:
Game -> hasMany plays
Play -> belongsTo game
我已经将以下内容添加到我的UrlMappings.groovy文件中。
"/games"(resources: "game") {
"/plays"(resources: "play")
}
使用Postman,我正在获得
http://localhost:8080/games
,并且得到了404
。如果删除UrlMappings并将
@Resource(uri='/games')
放在Game
域上,则它会返回200,这与预期的一样。这是我运行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 |
不知道为什么UrlMappings不起作用。
最佳答案
我的理解是UrlMappings语法"/games"(resources: "game")
自动生成关联的RESTful映射(并且仅生成映射),而使用@Resource(uri='/games')
语法生成 Controller / Action 。
使用"/games"(resources: "game")
,您仍然需要定义自己的 Controller 。您写这个问题的方式,让我觉得您还没有这样做。