createLink 到休息控制器

createLink to rest controllers

我已经定义了我的域对象

class Product implements Serializable{
    String sku
    static hasMany = [images: MediaContent]
}

class MediaContent implements Serializable{
    [...]
}

及其扩展 RestFulController 的 REST 控制器。具体来说,ProductRestController 如下所示: class ProductRestController 扩展 RestfulController {

static responseFormats = ['json']

ProductRestController(){
    super(Product)
    JSON.registerObjectMarshaller(Product){
    return [
               id: it.id,
               media: createLink(controller:"mediaRest", id:it.imageId)
    ]
}

url映射非常简单,因为

"/rest/product" (resources:"productRest")
"/rest/media" (resources:"mediaRest")

问题是使用 createLink 指令生成的 link 不是剩余格式 /rest/media/1,而是 /rest/media/index?id=1。 这两个 url 在调用时都有效,但只生成 ?id= 版本。 我也尝试过使用 resource 属性 创建链接,但没有任何效果。 我怎样才能做到这一点?

您可以在 UrlMappings.groovy

中进行更改
"/rest/product/$action/$id"(controller: 'productRest')

"/rest/media/$action/$id"(controller: 'mediaRest')