web-services - Grails:不同格式的RESTful Web服务数据处理

标签 web-services rest grails exchangewebservices groovyws

目前我正在使用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/

相关文章:

grails - Grails集成测试因 “Unsatisfied dependency expressed through constructor parameter”而失败

java - 向特定服务发送 SOAP 请求

asp.net - 如何在 .NET XML Web 服务 WSDL 中隐藏类型继承

grails - Grails从2.2.3升级到2.5.1

xml - REST - 何时使用 400 ("Bad Request")

Spring Data Rest 同一对象的不同数据取决于 uri/files <-> files/1

grails - 如何使用 Grails 获得测试覆盖率?

python - Django Choice 领域 Web 服务最佳实践

c# - ASMX Web 服务可以使用 Response.BinaryWrite 响应吗?

php - 使用 PHP 发送/接收 PUT 请求,在解析请求正文时遇到问题?