【发布时间】:2019-11-06 01:48:00
【问题描述】:
我正在使用 Zuul 后置过滤器来拦截响应。我的要求是在响应 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.
谁能帮我解决这个问题。
【问题讨论】:
标签: java spring-boot proxy netflix-zuul