如何在java中修改HttpServletRequest主体? [英] How to modify HttpServletRequest body in java?

查看:69
本文介绍了如何在java中修改HttpServletRequest主体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在它到达 Http Servlet 并被处理之前修改请求正文.请求正文的 JSON 有效负载如下所示,我想摆脱PayamtChqmanViewObject"(细节)部分.

I would like to modify the request body before it reaches to Http Servlet and gets processed.The JSON Payload of the Request body is like following and I would like to get rid of the "PayamtChqmanViewObject" (Detail) part.

{
      "ChqAccCode": "1",
      "ChqAmt": 1,
      "ChqBankCompCode": "TEST",
      "ChqBchName": "TEST",
      "ChqBchPost": "Y",
      "ChqPostDate":"2020-08-14",
      "ChqCompCode": "TEST",
      "ChqDate": "2020-04-21",
      "ChqDeptCode": "0",
      "ChqDesc": "TEST",
      "ChqDraftCode": "M",
      "ChqJobCode": null,
      "ChqJointVenName": null,
      "ChqNum": 123,
      "ChqPayeeAddr1": "Rome",
      "ChqPayeeAddr2": "1",
      "ChqPayeeAddr3": "Rome",
      "ChqPayeeCountry": "Italy",
      "ChqPayeeName1": "A1",
      "ChqPayeeName2": null,
      "ChqPayeePostalCode": "85695",
      "ChqPayeeRegCode": "IT",
      "ChqRecCode": "O",
      "ChqSeqNum": "1",
      "ChqVenCode": "ZZ",
      "ChqVouCode": null,
      "PayamtChqmanViewObj":[
        {
          "PaCompCode": "ZZ",
          "PaChqCompCode": "ZZ",
          "PaVenCode": "ACME",
          "PaChqNum": 123,
          "PaPayCurrAmt": 1,
          "PaAmt": 1,
          "PaVouInvCode": "INV001",
          "PaDiscAmt": 0,
          "PaChqSeqNum": "1"
        }
   ]
}

我可以使用以下方法获取请求正文,但是我不确定如何删除 JSON 的详细信息部分并将处理后的请求正文传递给 HTTP Servlet.

I am able to get the Request Body with using following method, however I am not sure how to delete the detail part of the JSON and pass the processed request body to HTTP Servlet.

 public static String getBody(HttpServletRequest request) throws IOException {

        String body = null;
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bufferedReader = null;

        try {
            InputStream inputStream = request.getInputStream();
            if (inputStream != null) {
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                char[] charBuffer = new char[128];
                int bytesRead = -1;
                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                    stringBuilder.append(charBuffer, 0, bytesRead);
                }
            } else {
                stringBuilder.append("");
            }
        } catch (IOException ex) {
            throw ex;
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException ex) {
                    throw ex;
                }
            }
        }

        body = stringBuilder.toString();
        System.out.println("BODY IS:" + body);
        return body;
    }

非常感谢您的帮助!

推荐答案

您无法更改请求,但可以对其进行包装.请参阅以下问题了解更多详情:我们为什么要包装HttpServletRequest ?api 提供了一个 HttpServletRequestWrapper 但是我们从包装请求中获得了什么?

You can't change the request, but you could wrap it. See the following question for more details: Why do we wrap HttpServletRequest ? The api provides an HttpServletRequestWrapper but what do we gain from wrapping the request?

您需要放置一个 servlet 过滤器 在您的 servlet 前面使包装工作.

You will need to put a servlet filter in front of your servlet to make the wrapping work.

至于如何从内容中删除该部分,您可以使用 String 类提供的普通旧字符串操作或类似 StringUtils,或者你可以使用您选择的库解析 JSON,删除该属性,然后将其写回字符串.

As for how to remove that part from the content, you could do it with plain old string manipulations from what the String class offers, or with something like StringUtils, or you could parse the JSON with a library of your choice, remove that property, then write it back as a string.

这篇关于如何在java中修改HttpServletRequest主体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆