Get RequestBody and ResponseBody at HandlerInterceptor

前端 未结 7 1762
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-14 11:31

Logic I have to implement is logging all requests with body served to DB.

So I decided to use: afterCompletion method of HandlerInterceptor

7条回答
  •  粉色の甜心
    2020-12-14 12:12

    You can get the request body from HTTPServlet request inside HandlerInterceptorAdapter methods using the method:

    request.getInputStream();
    

    If you want it to use with scanner you can assign the scanner as follows:

    Scanner scanner = new Scanner(request.getInputStream(), "UTF-8");
    

    Then you can use the required data from the scanner using some manipulation or scanner tricks. Or else you can assign it to a string also using Apache Commons IO:

    String requestStr = IOUtils.toString(request.getInputStream());
    

提交回复
热议问题