Grails 3 配置文件,JSON 渲染和一对多自我
Grails 3 profiles,JSON rendering and One to Many with self
我正在使用 gson 和网络配置文件。
我的域名是:
package json
import grails.rest.*
@Resource(readOnly = false, formats = ['json', 'xml'])
class Hero {
String name
String data
String relation
Book book
static hasMany = [children: Hero]
我的控制器是:
package json
import grails.rest.*
import grails.converters.*
class HeroController extends RestfulController {
static responseFormats = ['json', 'xml']
HeroController() {
super(Hero)
}
def show(Hero hero){
respond hero
}
}
我的gson:
hero.gson
import json.Hero
model {
Hero hero
}
json tmpl.hero(hero)
_hero.gson
import json.Hero
model {
Hero hero
}
json {
//data hero.data
id hero.id
data(relation: hero.relation)
name hero.name
children g.render(hero.children)
}
如果我 运行 使用 restful 配置文件,则所有子节点都会正确呈现。
如果我使用网络配置文件,则只呈现两层深度。
我的预期结果是:
{
"id": 4,
"data": {
"relation": "e"
},
"name": "e",
"children": [{
"id": 2,
"data": {
"relation": "c"
},
"name": "c",
"children": [{
"id": 1,
"data": {
"relation": "b"
},
"name": "b",
"children": []
}
]
}
是否可以进行一对多 json 渲染(与 restful 配置文件一样)?
有没有办法控制渲染的深度?
P.S。我阅读了文档,这部分对我来说不是很清楚:
If you wish for the author to be included as part of the rendering,
there are two requirements, first you must make sure the association
is initialized.
If the render method encounters a proxy, it will not traverse into the
relationship to avoid N+1 query performance problems. The same
applies to one-to-many collection associations. If the association has
not been initialized the render method will not traverse through the
collection!
So you must make sure your query uses a join:
Book.findByTitle("The Stand", [fetch:[author:"join"]])
Secondly when calling the render method you should pass the deep
argument:
json g.render(book, [deep:true])
是的,可以使用网络配置文件呈现您的 json。
我可以通过添加到 build.gradle:
apply plugin:"org.grails.plugins.views-json"
dependencies {
compile "org.grails.plugins:views-json"
compile "org.grails.plugins:views-json-templates"
bootRun {
jvmArgs('-Dspring.output.ansi.enabled=always')
addResources = true
}
并在域中稍作更改后使用您的代码 class:
children懒惰:假
@Resource(readOnly = false, formats = ['json', 'xml'])
class Hero {
String name
String data
String relation
//Book book
static hasMany = [children: Hero]
static mapping = {
children lazy: false
}
这将更改获取模式。
您可以在这里咨询有关提取的信息:
enter link description herehttp://docs.grails.org/3.1.1/ref/Database%20Mapping/fetch.html
我正在使用 gson 和网络配置文件。
我的域名是:
package json
import grails.rest.*
@Resource(readOnly = false, formats = ['json', 'xml'])
class Hero {
String name
String data
String relation
Book book
static hasMany = [children: Hero]
我的控制器是:
package json
import grails.rest.*
import grails.converters.*
class HeroController extends RestfulController {
static responseFormats = ['json', 'xml']
HeroController() {
super(Hero)
}
def show(Hero hero){
respond hero
}
}
我的gson:
hero.gson
import json.Hero
model {
Hero hero
}
json tmpl.hero(hero)
_hero.gson
import json.Hero
model {
Hero hero
}
json {
//data hero.data
id hero.id
data(relation: hero.relation)
name hero.name
children g.render(hero.children)
}
如果我 运行 使用 restful 配置文件,则所有子节点都会正确呈现。 如果我使用网络配置文件,则只呈现两层深度。
我的预期结果是:
{
"id": 4,
"data": {
"relation": "e"
},
"name": "e",
"children": [{
"id": 2,
"data": {
"relation": "c"
},
"name": "c",
"children": [{
"id": 1,
"data": {
"relation": "b"
},
"name": "b",
"children": []
}
]
}
是否可以进行一对多 json 渲染(与 restful 配置文件一样)? 有没有办法控制渲染的深度?
P.S。我阅读了文档,这部分对我来说不是很清楚:
If you wish for the author to be included as part of the rendering, there are two requirements, first you must make sure the association is initialized.
If the render method encounters a proxy, it will not traverse into the relationship to avoid N+1 query performance problems. The same applies to one-to-many collection associations. If the association has not been initialized the render method will not traverse through the collection!
So you must make sure your query uses a join:
Book.findByTitle("The Stand", [fetch:[author:"join"]])
Secondly when calling the render method you should pass the deep argument:
json g.render(book, [deep:true])
是的,可以使用网络配置文件呈现您的 json。 我可以通过添加到 build.gradle:
apply plugin:"org.grails.plugins.views-json"
dependencies {
compile "org.grails.plugins:views-json"
compile "org.grails.plugins:views-json-templates"
bootRun {
jvmArgs('-Dspring.output.ansi.enabled=always')
addResources = true
}
并在域中稍作更改后使用您的代码 class: children懒惰:假
@Resource(readOnly = false, formats = ['json', 'xml'])
class Hero {
String name
String data
String relation
//Book book
static hasMany = [children: Hero]
static mapping = {
children lazy: false
}
这将更改获取模式。 您可以在这里咨询有关提取的信息:
enter link description herehttp://docs.grails.org/3.1.1/ref/Database%20Mapping/fetch.html