我在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
任何解决方案?