DefaultModifiableMethodsLoggingFilter implements Filter { @Override public void doFilter(final ServletRequest req, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; if (httpMethods.contains(request.getMethod().toLowerCase())) { ResettableStreamHttpServletRequest wrappedRequest = new ResettableStreamHttpServletRequest(request); String requestBody = IOUtils.toString(wrappedRequest.getReader()).replaceAll("\\s*\\n\\s*", ""); LOGGER.info("{} method, request body: {}", request.getMethod(), requestBody); wrappedRequest.resetInputStream(); request = wrappedRequest; } chain.doFilter(request, response); }  @Override void doFilter(final ServletRequest req, final ServletResponse response, final FilterChain chain); @Override void destroy(); @Override void init(final FilterConfig config); void setHttpMethods(final String httpMethods);  }
@Test public void testDoFilter() throws Exception { String str = "{\"name\":\"name1\"}"; InputStream is = new ByteArrayInputStream(str.getBytes()); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); when(request.getMethod()).thenReturn("POST"); when(request.getReader()).thenReturn(reader); unit.doFilter(request, response, chain); verify(request).getReader(); verify(chain).doFilter(any(ResettableStreamHttpServletRequest.class), eq(response)); }
@Test public void testDoFilterWithGetMethod() throws Exception { when(request.getMethod()).thenReturn("GET"); unit.doFilter(request, response, chain); verify(request, times(0)).getReader(); verify(chain).doFilter(request, response); }