Groovy RestClient 没有为 Content-Type 使用正确的响应处理程序:application/json?

Groovy RestClient not using correct response handler for Content-Type: application/json?

我正在使用 Groovy 的 RestClient.java to POST some plain text to a server, the response I get back is JSON but I'm having some issues figuring out why the response is being parsed into a StringReader object instead of a JsonSlurper object。

根据我的理解,应该根据响应 header 中的内容类型将响应 body 解析为 object,响应 header 我回来了是 application/json 但它似乎默认为并将响应解析为 StringReader object。我不确定我是否错误地定义了我的 header 或我是否需要指定我自己的成功处理程序?我有其他使用 POST 方法并且工作正常的方法,但是他们正在发布 XML 并期待 XML 响应所以我认为 headers 在那里工作得很好实例.

下面是我的尝试方式和我的日志,你可以看到响应 headers Content-Typeapplication/json 所以我很困惑为什么 restClientResponse.getData()StringReader object 而不是 JsonSlurper object

def restClient = new RESTClient("http://this.doesnt.matter", 'text/plain')

restClient.setProxy("someproxy", 8080, "http")
restClient.setHeaders(Accept: "application/json")

def restClientResponse = restClient.post(body: "requestPayload:ACCOUNTPROFILE")
log.info(restClientResponse.getData())
log.info("What is in StringReader")

//Printing out what is in StringReader Object
def reader = restClientResponse.getData()
StringBuilder builder = new StringBuilder()
char[] characters = new char[1000]
builder.append(characters, 0, reader.read(characters, 0, characters.length))
log.info(builder.toString())

// Printing out each header in the response
restClientResponse.getAllHeaders().each {
    log.info("HEADER: " + it.getName() + ":" + it.getValue())
}

return restClientResponse.getData()

日志:

14 Apr 2015 17:25:04,561 uui.RestService java.io.StringReader@2a2e625
14 Apr 2015 17:25:04,562 uui.RestService What is in StringReader
14 Apr 2015 17:25:04,568 uui.RestService {"rbscResponse":{"errorMessage":"Unexpectederror","INSTALLATIONNUMBER":"","PRODUCTCOMMONNAME":"","errorCode":"30003","BTWSID":"","SERVICELINETYPE":"","CUSTOMERTYPE":"","RBSID":""}}
14 Apr 2015 17:25:04,590 uui.RestService HEADER: Date:Tue, 14 Apr 2015 16:28:20 GMT
14 Apr 2015 17:25:04,591 uui.RestService HEADER: Content-Type:application/json
14 Apr 2015 17:25:04,591 uui.RestService HEADER: X-Powered-By:Servlet/2.5 JSP/2.1
14 Apr 2015 17:25:04,591 uui.RestService HEADER: X-Cache:MISS from someproxy
14 Apr 2015 17:25:04,592 uui.RestService HEADER: X-Cache-Lookup:MISS from someproxy:8080
14 Apr 2015 17:25:04,592 uui.RestService HEADER: Via:1.0 someproxy (squid/3.1.10)
14 Apr 2015 17:25:04,593 uui.RestService HEADER: Connection:close

关于为什么响应 body 没有被解析为 JsonSlurper Object 的任何想法?

解决方案是从 RESTClient 构造函数中删除 Content-Type 参数,并在 post 参数中指定 requestContentType,如下所示:

def restClient = new RESTClient("http://this.doesnt.matter")
...
def restClientResponse = restClient.post(
    body:"requestPayload:ACCOUNTPROFILE",
    requestContentType: 'text/plain'
)

问题是我(缺乏)理解为 RESTClient 构造函数提供默认 Content-Type 的作用。我假设通过向 RESTClient 构造函数提供 text/plain 的 Content-Type,它会在 请求 [=37] 中将 Content-Type 设置为 text/plain =] 然后根据 响应 header 中的 Content-Type 计算出要用于响应 body 的解析器。但是,我认为通过为 RESTClient 提供 text/plain 参数,当它收到响应时,它会忽略响应 header 中的 Content-Type 并使用 Content-Type在 RESTClient 构造函数中提供。

感谢@dmahapatro 将我指向文档的this question which lead me to read the POST a status update to Twitter! 部分