如何在 Zuul post filter 中拦截和编辑响应体?

我正在使用 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.

谁能帮我解决这个问题。


千万里不及你
浏览 90回答 2
2回答

蓝山帝景

我遇到了同样的错误,疯狂地修改了How to get response body in Zuul post filter? 中描述的代码。尝试不同的可能性。最后,我在这篇文章中找到了解决方案,方法是在OutputStreamfromservletResponse.getOutputStream()而不是 中写下答案ctx.setResponseDataStream():HttpServletResponse servletResponse = ctx.getResponse();&nbsp; ...String updatedResponse = jsonObj.toString();try {&nbsp; &nbsp; OutputStream outStream = servletResponse.getOutputStream();&nbsp; &nbsp; outStream.write(updatedResponse.getBytes(), 0, updatedResponse.length());&nbsp; &nbsp; outStream.flush();&nbsp; &nbsp; outStream.close();} catch (IOException e) {&nbsp; &nbsp; log.warn("Error reading body", e);}

泛舟湖上清波郎朗

我有一个类似的任务,并试图通过写入 OutputStream 来完成。这有效,但有一个奇怪的副作用,它使响应中的 HttpHeaders 被删除或损坏。这使得调用在生产中产生 CORS 错误,即使它通过 Postman 在本地运行良好。我编写了以下方法,我从我的 Post Zuul 过滤器的 run() 方法调用该方法以将单个节点/值添加到返回的 Json。&nbsp; &nbsp; private void addJsonNode(RequestContext requestContext,String name, String id) {&nbsp; &nbsp; &nbsp; &nbsp; HttpServletResponse servletResponse = requestContext.getResponse();&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final InputStream responseDataStream = requestContext.getResponseDataStream();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String responseData = CharStreams.toString(new InputStreamReader(responseDataStream, "UTF-8"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObject = new JSONObject(responseData);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonObject.put(name, id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String updatedResponse = jsonObject.toString(4);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; requestContext.setResponseBody(updatedResponse);&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.warn("Error reading body", e);&nbsp; &nbsp; &nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.warn("Error reading body", e);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java