【问题标题】:REST resources defined in UrlMappings results in 404UrlMappings 中定义的 REST 资源导致 404
【发布时间】:2016-02-27 17:19:21
【问题描述】:

使用 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 并在 Game 域上放置一个 @Resource(uri='/games'),它将返回 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 不起作用。

【问题讨论】:

    标签: rest grails url-mapping grails-3.0


    【解决方案1】:

    我的理解是 UrlMappings 语法 "/games"(resources: "game") 自动生成关联的 RESTful 映射(并且只有映射),而使用 @Resource(uri='/games') 语法生成控制器/动作。

    使用"/games"(resources: "game"),您仍然需要定义自己的控制器。你写这个问题的方式,我感觉你还没有这样做。

    【讨论】:

    • 我没有。但文档另有建议。我会调查的。
    • 来自文档:如果您希望在 UrlMappings.groovy 文件中保留 URL 映射的声明,那么只需删除资源转换的 uri 属性并将以下行添加到 UrlMappings.groovy 就足够了.这意味着没有控制器,因为资源转换不需要控制器。所以要么文档是错误的,要么我没有正确地做其他事情。
    • 哦,快。我刚刚又读了一遍。域上仍然需要该资源。只是 URI 属性被删除了。
    • 当我第一次遇到文档时,我真的很困惑,我使用带有 URI 的 @Resource 注释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-21
    相关资源
    最近更新 更多