title: Spring-Boot processes HttpServletRequest before registered servlets
body: <p>The following code demonstrates the difference in <code>HttpServletRequest</code> processing a request between <code>Spring-Boot</code> and a stand-alone servlet container (aka. Tomcat or Jetty). Both work, however I am less than happy with the Spring variant.</p>
  
  <p>I have been unable to create a method to either copy the <code>InputStream</code> before it is processed by Spring or to suppress the processing of the request by Spring and just pass it "as is" to the Servlet. The servlet is interacting with a legacy API and needs to read/write bytes directly from the Request Body. Spring tries to map the payload, reads the InputStream and that requires the kludge of reading <code>ParameterNames</code> instead of just passing the Request Body on to the underlying socket to the legacy API. Any help turning this horrid spring code into something a bit more sane is greatly appreciated.</p>
  
  <p>Things I've already tried (not necessarily correctly) that haven't worked. Filters to copy the InputStream, various consumes and produces annotations, etc... I also played with <code>ByteArrayHttpMessageConverter</code> to no avail. </p>
  
  <p>The easiest solution in my opinion would be to flag the Servlet that works in <code>Tomcat</code> and just have Spring pass on the request via <code>ServletRegistrationBean</code> without modification. I'm sure I'm just missing something obvious here. </p>
  
  <pre><code>/* 
   * Works in Spring-Boot 1.2.5 
   */
  public void doIt(HttpServletRequest request, HttpServletResponse response)
  {       
      response.setContentType("application/octet-stream");
      response.setHeader("Cache-Control", "no-cache");
      response.setContentLength(0);
  
      Writer w = acquireWriter();
  
      Enumeration&lt;String&gt; an = request.getParameterNames();
  
      while(an.hasMoreElements()){
          char[] msg = an.nextElement().toCharArray();
          if(msg[0]=='w')
              continue;
          e.write(msg, 0, msg.length);
      }
  }
  
  @Bean
  public MyServlet myServlet(){
      return new MyServlet();
  }
  
  @Bean
  public ServletRegistrationBean servletRegistrationBean(){
      ServletRegistrationBean bean = new ServletRegistrationBean(myServlet(), "/op");
      bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
      return bean;
  }
  
  /*
   * Works in Tomcat 7/8 
   */
  public void doIt(HttpServletRequest request, HttpServletResponse response)
  {       
      response.setContentType("application/octet-stream");
      response.setHeader("Cache-Control", "no-cache");
      response.setContentLength(0);
  
      Writer w = acquireWriter();
      InputStreamReader input = new InputStreamReader(request.getInputStream(), "UTF-8");
  
      char[] buffer = new char[8192];
  
      int length=-1;
      while((length = input.read(buffer, 0, buffer.length)) != -1) {
          w.write(buffer, 0, length);
      }
  }
  </code></pre>