我正在使用 Zuul post filter 来拦截响应。我的要求是向响应 json 添加一个新字段。我能够拦截响应并对其进行编辑。但是,无法将更新后的响应设置为 RequestContext。在后过滤器中使用 Zuul 作为代理时,如何读取响应主体、编辑并将其更新回 RequestContext?
请找到我正在使用的以下代码。
private void updateResponseBody(RequestContext ctx) throws IOException, JSONException {
final InputStream responseDataStream = ctx.getResponseDataStream();
String responseData = CharStreams.toString(new InputStreamReader(responseDataStream, "UTF-8"));
JSONObject jsonObj = new JSONObject(responseData);
JSONArray groupsArray = jsonObj.getJSONArray("list");
for (int i = 0; i < groupsArray.length(); i++) {
JSONObject groupId = groupsArray.getJSONObject(i);
groupId.accumulate("new_json_field_name", "new_json_field_value");
}
String updatedResponse = jsonObj.toString();
// ctx.setResponseBody(body); // also not working
ctx.setResponseDataStream(org.apache.commons.io.IOUtils.toInputStream(updatedResponse, "UTF-8"));
}
我得到的错误是:
Error while sending response to client: java.io.IOException: An existing connection was forcibly closed by the remote host.
谁能帮我解决这个问题。
蓝山帝景
泛舟湖上清波郎朗
相关分类