Get RequestBody and ResponseBody at HandlerInterceptor

前端 未结 7 1792
爱一瞬间的悲伤
爱一瞬间的悲伤 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:07

    This is quite an old thread but for the sake of people who looking for a way to get RequestBody and ResponseBody from Interceptor. Here is how I got it working.

    RequestBody, can simply use IOUtils:

    String requestBody= IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
    

    ResponseBody, I had to used HttpServletResponseCopier:

    ServletLoggingFilter.HttpServletResponseCopier copier = (ServletLoggingFilter.HttpServletResponseCopier) response;
    String responseBody = new String(copier.getCopy());
    

提交回复
热议问题