title: spring boot RestController get HttpServletResponse content
body: <p>I use spring boot build project, RestController return string data.</p>
  
  <p>I want get response content in Filter.</p>
  
  <p>But cant get, please help me.</p>
  
  <p>controller:</p>
  
  <pre><code>@RestController
  @RequestMapping(value = "/service/example")
  public class ExampleController {
      @RequestMapping(value = "/get/test", method = RequestMethod.POST)
      public String message(@RequestBody String data) {
          return "test";
      }
  
      @RequestMapping(value = "/get/test1", method = RequestMethod.POST)
      public void message(HttpServletRequest request, HttpServletResponse response) throws IOException {
          PrintWriter writer = response.getWriter();
          writer.write("dfsfd");
          writer.flush();
      }
  }
  </code></pre>
  
  <p>filter:</p>
  
  <pre><code>@WebFilter(filterName="myFilter",urlPatterns="/service/*")
  public class MyFilter extends OncePerRequestFilter {
      @Override
      protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
          throws ServletException, IOException {
          MyHttpServletResponseWrapper responseWrapper = new MyHttpServletResponseWrapper(response);
          filterChain.doFilter(request, responseWrapper);
          String responseContent = responseWrapper.getContent();
          System.out.println("response="+responseContent);
      }
  }
  </code></pre>
  
  <p>MyHttpServletResponseWrapper :</p>
  
  <pre><code>public class MyHttpServletResponseWrapper extends HttpServletResponseWrapper {
  
      private PrintWriter cachedWriter;
      private CharArrayWriter bufferedWriter;
  
      /**
       * Constructs a response adaptor wrapping the given response.
       *
       * @param response The response to be wrapped
       * @throws IllegalArgumentException if the response is null
       */
      public MyHttpServletResponseWrapper(HttpServletResponse response) {
          super(response);
          bufferedWriter = new CharArrayWriter();
          cachedWriter = new PrintWriter(bufferedWriter);
      }
  
      @Override
      public PrintWriter getWriter() throws IOException {
          return cachedWriter;
      }
  
      /**
       * 获取原始HTML
       *
       * @return
       */
      public String getContent() {
          byte[] bytes = bufferedWriter.toString().getBytes();
          try {
              return new String(bytes, "UTF-8");
          } catch (UnsupportedEncodingException e) {
              return "";
          }
      }
  }
  </code></pre>
  
  <p>post to /service/example/get/test cant get content.
  but post to /service/example/get/test1 can get content.</p>
  
  <p>why?</p>
  
  <p>My project has many rest like /service/example/get/test, I dont want to change each one.</p>
  
  <p>how to get response content in filter, please help, Thanks!!!</p>