如何使用Grails REST客户端构建器插件指定内容类型?

时间:2013-12-17 09:10:25

标签: rest grails

我正在使用Grails REST客户端构建器插件(https://github.com/grails-plugins/grails-rest-client-builder),如下所示:

final def rest = new RestBuilder(connectTimeout:connectTimeout, readTimeout:readTimeout)
final def resp = rest.get(uri) 

因此服务器总是以XML格式返回结果 - 我如何(客户端)指定我希望结果为JSON?

2 个答案:

答案 0 :(得分:2)

通过contentType方法设置内容类型。 documetnation通过以下示例指出了这一点。

        def resp = rest.put("http://repo.grails.org/grails/api/security/groups/test-group"){
        auth System.getProperty("artifactory.user"), System.getProperty("artifactory.pass")
        contentType "application/vnd.org.jfrog.artifactory.security.Group+json"
        json {
            name = "test-group"
            description = "A temporary test group"
        }
    }

答案 1 :(得分:1)

试试这样:

final def rest = new RestBuilder(connectTimeout:connectTimeout, readTimeout:readTimeout)
final def resp = rest.get(uri) {
  accept "application/json"  # Add this bit
}