亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Zuul post filter 中攔截和編輯響應體?

如何在 Zuul post filter 中攔截和編輯響應體?

千萬里不及你 2023-02-16 17:19:16
我正在使用 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.誰能幫我解決這個問題。
查看完整描述

2 回答

?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

我遇到了同樣的錯誤,瘋狂地修改了How to get response body in Zuul post filter? 中描述的代碼。嘗試不同的可能性。最后,我在這篇文章中找到了解決方案,方法是在OutputStreamfromservletResponse.getOutputStream()而不是 中寫下答案ctx.setResponseDataStream()


HttpServletResponse servletResponse = ctx.getResponse();


  ...


String updatedResponse = jsonObj.toString();

try {

    OutputStream outStream = servletResponse.getOutputStream();

    outStream.write(updatedResponse.getBytes(), 0, updatedResponse.length());

    outStream.flush();

    outStream.close();

} catch (IOException e) {

    log.warn("Error reading body", e);

}


查看完整回答
反對 回復 2023-02-16
?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

我有一個類似的任務,并試圖通過寫入 OutputStream 來完成。這有效,但有一個奇怪的副作用,它使響應中的 HttpHeaders 被刪除或損壞。這使得調用在生產中產生 CORS 錯誤,即使它通過 Postman 在本地運行良好。


我編寫了以下方法,我從我的 Post Zuul 過濾器的 run() 方法調用該方法以將單個節點/值添加到返回的 Json。


    private void addJsonNode(RequestContext requestContext,String name, String id) {

        HttpServletResponse servletResponse = requestContext.getResponse();

        try {

            final InputStream responseDataStream = requestContext.getResponseDataStream();

            String responseData = CharStreams.toString(new InputStreamReader(responseDataStream, "UTF-8"));

            JSONObject jsonObject = new JSONObject(responseData);

            jsonObject.put(name, id);

            String updatedResponse = jsonObject.toString(4);

            requestContext.setResponseBody(updatedResponse);

        } catch (IOException e) {

            log.warn("Error reading body", e);

        } catch (JSONException e) {

            log.warn("Error reading body", e);

        }

    }


查看完整回答
反對 回復 2023-02-16
  • 2 回答
  • 0 關注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號