6.1.7 XML和JSON响应 - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith
Version: null
6.1.7 XML和JSON响应
Using the render method to output XML
Grails supports a few different ways to produce XML and JSON responses. The first is the render method.Therender method can be passed a block of code to do mark-up building in XML:def list() { def results = Book.list() render(contentType: "text/xml") {
books {
for (b in results) {
book(title: b.title)
}
}
}
}<books> <book title="The Stand" /> <book title="The Shining" /> </books>
def list() { def books = Book.list() // naming conflict here render(contentType: "text/xml") {
books {
for (b in results) {
book(title: b.title)
}
}
}
}books which Groovy attempts to invoke as a method.
使用render方法输出XML
Grails支持几种不同的方法来产生XML和JSON响应。第一个就是render方法。render方法可以被传递一个代码块,在代码块中,使用标签生成器来构建XML:def list() { def results = Book.list() render(contentType: "text/xml") {
books {
for (b in results) {
book(title: b.title)
}
}
}
}<books> <book title="The Stand" /> <book title="The Shining" /> </books>
def list() { def books = Book.list() // naming conflict here render(contentType: "text/xml") {
books {
for (b in results) {
book(title: b.title)
}
}
}
}books试图被当作一个方法来调用。Using the render method to output JSON
Therender method can also be used to output JSON:def list() { def results = Book.list() render(contentType: "text/json") {
books = array {
for (b in results) {
book title: b.title
}
}
}
}[
{title:"The Stand"},
{title:"The Shining"}
]使用render方法输出JSON
render方法也可以被用来输出JSON:def list() { def results = Book.list() render(contentType: "text/json") {
books = array {
for (b in results) {
book title: b.title
}
}
}
}[
{title:"The Stand"},
{title:"The Shining"}
]Automatic XML Marshalling
Grails also supports automatic marshalling of domain classes to XML using special converters.To start off with, import thegrails.converters package into your controller:import grails.converters.*render Book.list() as XML
<?xml version="1.0" encoding="ISO-8859-1"?> <list> <book id="1"> <author>Stephen King</author> <title>The Stand</title> </book> <book id="2"> <author>Stephen King</author> <title>The Shining</title> </book> </list>
def xml = Book.list().encodeAsXML() render xml
自动XML编组(Marshalling)
Grails还支持将领域类自动编组为XML的用法,不过要借助于特定的转换器。首先,要先在你的控制器中的导入包grails.converters:import grails.converters.*render Book.list() as XML
<?xml version="1.0" encoding="ISO-8859-1"?> <list> <book id="1"> <author>Stephen King</author> <title>The Stand</title> </book> <book id="2"> <author>Stephen King</author> <title>The Shining</title> </book> </list>
def xml = Book.list().encodeAsXML() render xml
Automatic JSON Marshalling
Grails also supports automatic marshalling to JSON using the same mechanism. Simply substituteXML with JSON:render Book.list() as JSON
[
{"id":1,
"class":"Book",
"author":"Stephen King",
"title":"The Stand"},
{"id":2,
"class":"Book",
"author":"Stephen King",
"releaseDate":new Date(1194127343161),
"title":"The Shining"}
]encodeAsJSON to achieve the same effect.
自动JSON编组
Grails同样也支持自动JSON编组的功能,这跟XML机制完全相同,因此只需要简单地将XML替换为JSON即可:render Book.list() as JSON
[
{"id":1,
"class":"Book",
"author":"Stephen King",
"title":"The Stand"},
{"id":2,
"class":"Book",
"author":"Stephen King",
"releaseDate":new Date(1194127343161),
"title":"The Shining"}
]encodeAsJSON来达到相同的效果。

