Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.6k views
in Technique[技术] by (71.8m points)

http - How to modify HttpServletRequest body in java?

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"
        }
   ]
}

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;
    }

Thank you very much for the help!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...