1

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!

4
  • Why do you need to delete that part?
    – Bogdan
    Commented Jan 12, 2021 at 20:33
  • It is needed for a certain business case.
    – obayral
    Commented Jan 12, 2021 at 20:34
  • 1
    @obayral - your approach is completely wrong. The Servlet specification already has a robust mechanism for exactly what you're trying to accomplish. Please review Bogdan's response below. Please post back if you have questions; please be sure to "Upvote" and "Accept " his reply.
    – paulsm4
    Commented Jan 12, 2021 at 21:05
  • @paulsm4 could you tell me why my approach is completely wrong? I have accepted the answer also.
    – obayral
    Commented Jan 12, 2021 at 21:15

1 Answer 1

3

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.

2
  • Thanks for the answer! It gave me a better insight. I do have 2 follow up questions though. 1) I can't change the request, but can't I wrap the request into a form that I want? 2) I can do a classical parsing and String manipulations(this was my 1st approach also), however how can I pass this String to HttpRequest to be processed by the Servlet? Thanks.
    – obayral
    Commented Jan 12, 2021 at 21:17
  • 1
    1) that's exactly what I'm saying in the answer. You wrap the original request into your own version with the help of HttpServletRequestWrapper. In your version you change what you want different and let everything else go through to the original request object you wrapped. 2) You will probably need to implement the getInputStream method in your wrapper to return an InputStream constructed from your String (maybe some other methods from the wrapper too, depending on what else you need). You then send the wrapper request to your servlet in the chain, not the original request.
    – Bogdan
    Commented Jan 13, 2021 at 9:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.