使用Grails RestfulController和DocumentBuilder

时间:2014-05-22 18:56:44

标签: xml rest grails groovy

我试图通过grails restful控制器返回一些XML指令并遇到一些问题。控制器本身不代表任何域类,我只想返回一些预先格式化的XML。下面我简单地使用groovy网站上的示例。

class GetFieldTypesController extends RestfulController {
static responseFormats = ['xml']

def index() { 

def builder  = DocumentBuilderFactory.newInstance().newDocumentBuilder()
def document = builder.newDocument()
def root     = document.createElement('records')

document.appendChild(root)
addCar(document, root, 'Australia',
   'Production Pickup Truck with speed of 271kph')
addCar(document, root,  'Isle of Man',
    'Smallest Street-Legal Car at 99cm wide and 59 kg in weight')
addCar(document, root,  'France',
     'Most Valuable Car at $15 million')

print root    
respond root

}

def addCar(document, root,  country,  text) {
def car = document.createElement('car')
root.appendChild(car)
def countryNode = document.createElement('country')
countryNode.appendChild(document.createTextNode(country))
car.appendChild(countryNode)
def record = document.createElement('record')
record.appendChild(document.createTextNode(text))
car.appendChild(record)
}    
}

打印输出看起来格式正确:

<?xml version="1.0" encoding="UTF-8"?><records>
<car>
<country>Australia</country>
<record>Production Pickup Truck with speed of 271kph</record>
</car>
<car>
<country>Isle of Man</country>
<record>Smallest Street-Legal Car at 99cm wide and 59 kg in weight</record>
</car>
<car>
<country>France</country>
<record>Most Valuable Car at $15 million</record>
</car>
</records>

但是当我点击REST网址时,我收到的字符串索引超出了范围错误。

    2014-05-22 11:44:11,659 [http-bio-8080-exec-8] ERROR errors.GrailsExceptionResolver  -      StringIndexOutOfBoundsException occurred when processing request: [GET] /compoundservice/getFieldTypes
String index out of range: -1. Stacktrace follows:
Message: String index out of range: -1
    Line | Method
->>  872 | substring                  in java.lang.AbstractStringBuilder
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     72 | substring                  in java.lang.StringBuilder
|    254 | handleCircularRelationship in grails.converters.XML
|    175 | convertAnother             in     ''
|    183 | convertAnother . . . . . . in     ''
|    125 | render                     in     ''
|    122 | renderXml . . . . . . . .  in org.grails.plugins.web.rest.render.xml.DefaultXmlRenderer
|    110 | renderXml                  in     ''
|     89 | render . . . . . . . . . . in     ''
|    177 | respond                    in org.grails.plugins.web.rest.api.ControllersRestApi
|     26 | index . . . . . . . . . .  in compoundservice.GetFieldTypesController
|    198 | doFilter                   in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter . . . . . . . . . in grails.plugin.cache.web.filter.AbstractFilter
|   1145 | runWorker                  in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . . . . . . . . .  in java.util.concurrent.ThreadPoolExecutor$Worker
^    724 | run                        in java.lang.Thread

我的第一个想法是Grails需要一个对象来返回XML(例如文档中的书籍示例),但我希望避免这种情况,因为我需要生成的XML有点嵌套,我希望能够生成它并通过控制器返回。任何有关如何正确执行此操作的帮助或建议将不胜感激。感谢。

1 个答案:

答案 0 :(得分:0)

您始终可以使用API​​工具包。它不依赖于GORM Object,而是绑定到ModelMap(返回的数据集),允许返回任何内容。它极大地简化了API开发,并将API抽象到前端控制器,从而可以更快地处理请求/响应检查。