如何获得Groovy RestClient失败响应的完整响应

如何获得Groovy RestClient失败响应的完整响应,groovy,rest-client,Groovy,Rest Client,目前,我得到了HttpResponseException,它只有状态码。 我怎样才能得到完整的回答 这是我正在使用的代码 restClient = new RESTClient("http://${Server}") try { HttpResponseDecorator resp = restClient.post(path,body,requestContentType) as HttpResponseDecorator return JSONObj

目前,我得到了HttpResponseException,它只有状态码。 我怎样才能得到完整的回答

这是我正在使用的代码

restClient = new RESTClient("http://${Server}")
try {
    HttpResponseDecorator resp = restClient.post(path,body,requestContentType)     
        as HttpResponseDecorator
    return JSONObject.fromObject(resp.getData()).get("topKey","");
    }
catch (HttpResponseException e) {
            error(e.toString())
    }
它只输出这个:

[oaf.error] groovyx.net.http.HttpResponseException: Internal Server Error

添加自定义失败响应处理程序:

        restClient = new RESTClient("http://${Server}")
        restClient.handler.failure = { resp, data ->
            resp.setData(data)
            String headers = ""
            resp.headers.each {
                headers = headers+"${it.name} : ${it.value}\n"
            }
            throw new HttpResponseException(resp.getStatus(),"HTTP call failed. Status code: ${resp.getStatus()}\n${headers}\n"+
                                            "Response: "+(resp as HttpResponseDecorator).getData())
        }

实际上,您可以从抛出的异常中提取完整响应。例如,如果捕获的异常是
e
,响应正文JSON应该包含一个名为
myCustomErrorCode
的字段,那么除了
e.response.data.myCustomErrorCode
之外,您还可以通过查看
e.statusCode

来检查其值,因为前一行抛出了一个异常。@jaco0646,您是对的。我删除了“return”语句。有关响应的详细信息,请参见例外。可能resp也应该设置为某个类变量……我觉得奇怪的是,
resp
中还没有包含
data
,因为
resp.setData()
是一个非公共方法。