如何在Grails中的HAL实现中自定义自链接?

时间:2019-06-28 11:48:33

标签: spring api grails hal

我在grails rest-api项目中有一个HAL实现。

我需要将默认链接更改为实现HATEOS。

我的域类:

@Linkable
class Province implements Serializable {

String name
String capital
Country country

static belongsTo = [country: Country]

static hasMany = [district: District]

static constraints = {
    id composite: ['id', 'country']
}
}

我的控制器:

//    @Override
def show(Integer country, Integer id){
//        Province province = Province.findByIdAndCountry(id, Country.findById(country))
//        province.link rel:'self', href: g.createLink(absolute: true, resource:"province", params:[countryId: country, provinceId: id])
   respond Province.findByIdAndCountry(id, Country.findById(country))
}

当前输出:

{
"_links": {
    "self": {
        "href": "http://localhost:9002/province/show/1",
        "hreflang": "en_US",
        "type": "application/hal+json"
    }
},
"id": 1,
"name": "Province One",
"capital": "Illam"
}

但是我想将href更改为以下内容:

 "self": {
        "href": "http://localhost:9002/province/{id}/{subId}",
        "hreflang": "en_US",
        "type": "application/hal+json"
    }

在控制器类中,如果我更改为

def show(Integer country, Integer id){
    Province province = Province.findByIdAndCountry(id, Country.findById(country))
    province.link rel:'self', href: g.createLink(absolute: true, resource:"province", params:[countryId: country, provinceId: id])

    respond province
}

我收到以下错误:

  

没有这样的属性:g用于类:core.restful.api.ProvinceController

任何解决方案?

0 个答案:

没有答案