1

I want to create a logger that put's all the post data into my logfile. I added a interceptor to Spring and added all the necessary stuff to the preHandle method where I am reading out the post data (most of the time it is raw json in my case)

final StringBuilder sb = new StringBuilder(128);
if (request.getMethod().equals("POST")) {
    sb.append("] [data=");
    StringBuffer jb = new StringBuffer();
    String line = null;
    try {
        BufferedReader reader = request.getReader();
        while ((line = reader.readLine()) != null)
        jb.append(line);
        reader.reset();
    } catch (Exception e) { 
    }
    sb.append(jb.toString());
}

Most of the code is taken from HttpServletRequest get JSON POST data where you can read from the comments that you can only read out the post data once! If you do that before the request is finalized you get an exception

java.lang.IllegalStateException: READER

How can I read out the POST data from the request without breaking any further actions?

Community
  • 1
  • 1
MatthiasLaug
  • 2,924
  • 6
  • 28
  • 43

0 Answers0