TeeHttpServletResponse extends HttpServletResponseWrapper implements BodyLogger { @Override public String getBody() { byte[] buff = getOutputBuffer(); if (buff != null) { if (ServletLogger.showAsText(getContentType())) { return new String(buff); } return StringUtils.abbreviate(Util.toBase64(buff), 100); } return ""; } TeeHttpServletResponse(HttpServletResponse httpServletResponse); @Override ServletOutputStream getOutputStream(); @Override PrintWriter getWriter(); @Override void flushBuffer(); byte[] getOutputBuffer(); void finish(); @Override String getBody(); @Override void setHeader(String name, String value); @Override void addHeader(String name, String value); Map<String, List<String>> getHeaders(); @Override void setStatus(int status); @Override void setStatus(int status, String sm); int getStatus();  }
@Test public void getBodyTest() throws IOException { TeeHttpServletResponse tee = new TeeHttpServletResponse(resp); tee.getOutputStream().write("this is my body".getBytes()); Map<String, Boolean> types = new HashMap<>(); types.put(MediaType.APPLICATION_JSON, true); types.put(MediaType.APPLICATION_ATOM_XML, true); types.put(MediaType.TEXT_PLAIN, true); types.put(MediaType.TEXT_HTML, true); types.put(MediaType.TEXT_XML, true); types.put(MediaType.APPLICATION_FORM_URLENCODED, true); types.put(MediaType.APPLICATION_OCTET_STREAM, false); types.put("multipart/form-data", false); types.put("application/zip", false); for (String type : types.keySet()) { when(resp.getContentType()).thenReturn(type); if (types.get(type)) { assertEquals("this is my body", tee.getBody()); } else { assertEquals(Util.toBase64("this is my body".getBytes()), tee.getBody()); } } }