目前我正在使用GGTS 3.6.0开发Grails 2.4.3
要求-grails Web服务如何工作。
现有-当前,我的封包对我来说是作为Web服务运行的,但输出特定于格式(一次输出JSON或XML)。
问题-在闭包(Web服务)中,我将如何返回JSON / XML和其他格式。
关闭代码-
def able_Webservice = {
ableService.populateAbleBean(ableBean);
ableService.settingWhereClause(ableBean);
ableService.getDBData(ableBean);
def jsonData = ableService.webservice_Data(ableBean);
render jsonData as JSON
}
最佳答案
Grails具有withFormat功能。您可以根据传入的请求Accept header ,格式参数或URI扩展来呈现不同的响应。
您的代码如下:
def able_Webservice = {
ableService.populateAbleBean(ableBean);
ableService.settingWhereClause(ableBean);
ableService.getDBData(ableBean);
def data = ableService.webservice_Data(ableBean);
withFormat {
xml { render data as XML }
json { render data as JSON }
}
}
这使用内置的content negotiation。
关于web-services - Grails:不同格式的RESTful Web服务数据处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27636788/