我正在编写带有grails的REST API,并且需要在方法的路径上放置一个变量。调用此方法(删除)的URL将如下所示:
http://localhost:8080/profiles/576/gameLibrary/591
我的URLMappings类中包含以下内容:
"/gameLibrary/${inventoryId}/"(controller: "profile", action: "removeFromLibrary", method: "DELETE")
我尝试过类似" $ inventory"的映射。没有大括号,并留下尾随斜线。当我尝试拨打此网址时,我收到了404.我已创建了网址报告,我看到了相关地图的以下内容:
控制器:个人资料
|删除| / profiles / $ {profileId} / gameLibrary / null |行动:removeFromLibrary |
所以,它就像尝试插入" $ {inventoryId}而不是将其用作路径变量。
也许我误解了如何创建路径变量。我认为嵌入变量文档的这一部分描述了我采用的方法:http://docs.grails.org/latest/guide/theWebLayer.html#embeddedVariables。
为了防万一,发布我的整个UrlMappings类:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?(.$format)?"{
constraints {
// apply constraints here
}
}
"/games"(resources:"game")
"/gameGenres"(resources:"gameGenre")
"/siteUsers"(resources:"siteUser")
"/profiles"(resources:"profile") {
"/gameLibrary" (controller: "profile", action: "getGameLibrary", method: "GET")
"/gameLibrary" (controller: "profile", action: "addGameToLibrary", method: "PUT")
"/gameLibrary/${inventoryId}/"(controller: "profile", action: "removeFromLibrary", method: "DELETE")
"/wishList" (controller: "profile", action: "getWishList", method: "GET")
}
"/inventoryItems"(resources:"inventoryItem")
"/"(view:"/index")
"500"(view:'/error')
"404"(view:'/notFound')
}
}
非常感谢任何帮助。