如何使用grailsrestclientbuilder插件指定内容类型?

如何使用grailsrestclientbuilder插件指定内容类型?,rest,grails,Rest,Grails,我正在使用Grails REST client builder插件(),如下所示: 因此服务器总是以XML的形式返回结果——我(客户端)如何指定我希望结果为JSON?通过contentType方法设置内容类型。下面的例子说明了这一点 def resp = rest.put("http://repo.grails.org/grails/api/security/groups/test-group"){ auth System.getProperty("artifac

我正在使用Grails REST client builder插件(),如下所示:


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

通过contentType方法设置内容类型。下面的例子说明了这一点

        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"
        }
    }

通过contentType方法设置内容类型。下面的例子说明了这一点

        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"
        }
    }

通过contentType方法设置内容类型。下面的例子说明了这一点

        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"
        }
    }

通过contentType方法设置内容类型。下面的例子说明了这一点

        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"
        }
    }
试着这样做:

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

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

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

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

“contentType”应该完成这项工作。但是服务提供商支持JSON吗?如果没有,那么您必须将XML转换为JSON,或者改用XML。但是服务提供商支持JSON吗?如果没有,那么您必须将XML转换为JSON,或者改用XML。但是服务提供商支持JSON吗?如果没有,那么您必须将XML转换为JSON,或者改用XML。但是服务提供商支持JSON吗?如果没有,那么您必须将XML转换为JSON,或者改用XML。您能解释一下这个解决方案比公认答案有什么优势吗?公认答案对我不起作用。我认为被接受的答案只适用于“PUT”。对于“GET”,您需要使用“accept”。在我的例子中,我得到的是JSON,但需要XML……当然,接受
application/JSON
意味着你得到的是JSON,而不是XML。此外,HTTP方法不应影响返回的有效负载。有趣的是,它对你来说是这样的。你能解释一下这个解决方案比被接受的答案有什么优势吗?被接受的答案对我不起作用。我认为被接受的答案只适用于“PUT”。对于“GET”,您需要使用“accept”。在我的例子中,我得到的是JSON,但需要XML……当然,接受
application/JSON
意味着你得到的是JSON,而不是XML。此外,HTTP方法不应影响返回的有效负载。有趣的是,它对你来说是这样的。你能解释一下这个解决方案比被接受的答案有什么优势吗?被接受的答案对我不起作用。我认为被接受的答案只适用于“PUT”。对于“GET”,您需要使用“accept”。在我的例子中,我得到的是JSON,但需要XML……当然,接受
application/JSON
意味着你得到的是JSON,而不是XML。此外,HTTP方法不应影响返回的有效负载。有趣的是,它对你来说是这样的。你能解释一下这个解决方案比被接受的答案有什么优势吗?被接受的答案对我不起作用。我认为被接受的答案只适用于“PUT”。对于“GET”,您需要使用“accept”。在我的例子中,我得到的是JSON,但需要XML……当然,接受
application/JSON
意味着你得到的是JSON,而不是XML。此外,HTTP方法不应影响返回的有效负载。有趣的是,它这样对你有效。