(Quick Reference)

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.

The render 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) } } } }

The result of this code would be something like:

<books>
    <book title="The Stand" />
    <book title="The Shining" />
</books>

Be careful to avoid naming conflicts when using mark-up building. For example this code would produce an error:

def list() {

def books = Book.list() // naming conflict here

render(contentType: "text/xml") { books { for (b in results) { book(title: b.title) } } } }

This is because there is local variable 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) } } } }

这是因为Groovy的本地变量books试图被当作一个方法来调用。

Using the render method to output JSON

The render 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 } } } }

In this case the result would be something along the lines of:

[
    {title:"The Stand"},
    {title:"The Shining"}
]

The same dangers with naming conflicts described above for XML also apply to JSON building.

使用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"}
]

注意!上面XML命名冲突的危险同样适用于JSON。

Automatic XML Marshalling

Grails also supports automatic marshalling of domain classes to XML using special converters.

To start off with, import the grails.converters package into your controller:

import grails.converters.*

Now you can use the following highly readable syntax to automatically convert domain classes to XML:

render Book.list() as XML

The resulting output would look something like the following::

<?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>

An alternative to using the converters is to use the codecs feature of Grails. The codecs feature provides encodeAsXML and encodeAsJSON methods:

def xml = Book.list().encodeAsXML()
render xml

For more information on XML marshalling see the section on REST

自动XML编组(Marshalling)

Grails还支持将领域类自动编组为XML的用法,不过要借助于特定的转换器。

首先,要先在你的控制器中的导入包grails.converters

import grails.converters.*

现在,你可以使用如下易读性高的语法来将领域类自动转换成XML:

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>

另外一种转换器的用法是使用Grails的编码(codecs)功能。此功能提供了encodeAsXMLencodeAsJSON方法:

def xml = Book.list().encodeAsXML()
render xml

更多关于XML编组信息请参考REST章节

Automatic JSON Marshalling

Grails also supports automatic marshalling to JSON using the same mechanism. Simply substitute XML with JSON:

render Book.list() as JSON

The resulting output would look something like the following:

[
    {"id":1,
     "class":"Book",
     "author":"Stephen King",
     "title":"The Stand"},
    {"id":2,
     "class":"Book",
     "author":"Stephen King",
     "releaseDate":new Date(1194127343161),
     "title":"The Shining"}
 ]

Again as an alternative you can use the 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来达到相同的效果。