进一步深入研究,我们发现必须对 HTTPBuilder 进行更改才能继续获得 0.5.2 将提供的未格式化响应。
旧 Groovy 代码片段:
// perform a GET request, expecting JSON response data
http.request(Method.GET, ContentType.JSON) { req ->
uri.path = restPath;
requestContentType = JSON;
uri.query = mapQuery;
headers.Accept = 'application/json';
// response handler for a successful response code:
response.success = { resp, json ->
// Check returned status'
assertResp(resp, expStatus);
return json.toString();
}
// Check returned status' for the 400's and 500's
response.failure = { resp ->
// Check returned status'
assertResp(resp, expStatus);
}
HTTPBuilder 文档表明自动格式化将用于 JSON 和 XML 等已知类型。为了关闭此功能,必须在 http.request 中指定 TEXT 的 ContentType。但是当这完成时,“json”对象的 toString 值(在 response.success 闭包中)不再返回返回的 JSON 值。这些值可以在“text”属性中找到。
所以最终的代码是这样的:
// perform a GET request, expecting JSON response data
http.request(Method.GET, ContentType.TEXT) { req ->
uri.path = restPath;
requestContentType = JSON;
uri.query = mapQuery;
headers.Accept = 'application/json';
// response handler for a successful response code:
response.success = { resp, json ->
// Check returned status'
assertResp(resp, expStatus);
return json.text;
}
// Check returned status' for the 400's and 500's
response.failure = { resp ->
// Check returned status'
assertResp(resp, expStatus);
}
希望如果有人遇到同样的问题,他们会咨询 Stack Overflow,他们会发现这些笔记很有帮助。此致