title: How can I log all of the request and header information for a Spring @RestController @RequestMapping method?
body: <p>We have a Spring method to handle a REST call that we're having some issues with depending on which clients we're using (browser vs mobile application). We'd like to be able to see the raw request and headers but haven't been able to figure out how to easily do that. The best we've come up with is to add the <code>HttpServletRequest</code> to our parameters in the method and create a long method to print out the various parts of the request object. Is there a better way like turning on debug logging for some specific <code>org.springframework.web.*</code> Spring class?</p>
  
  <p>An edited version of our method and the <code>printRequestInfo()</code> method is:</p>
  
  <pre><code>@RequestMapping(method = RequestMethod.POST, value = "/test/{testId}")
  public void doSomething(@PathVariable Long testId,
          @RequestParam(value = "someOtherParam", required = false) String someOtherParam,
          HttpServletRequest req)
  {
      printRequestInfo(req);
      // ...        
  }
  
  private void printRequestInfo(HttpServletRequest req) {
      StringBuffer requestURL = req.getRequestURL();
      String queryString = req.getQueryString();
  
      if (queryString == null) {
          logger.info("url: " + requestURL.toString());
      } else {
          logger.info("url: " + requestURL.append('?').append(queryString).toString());
      }
  
      logger.info( "method:" + req.getMethod());
  
      // print all the headers
      Enumeration headerNames = req.getHeaderNames();
      while(headerNames.hasMoreElements()) {
          String headerName = (String)headerNames.nextElement();
          logger.info("header: " + headerName + ":" + req.getHeader(headerName));
      }
  
      // print all the request params
      Enumeration params = req.getParameterNames();
      while(params.hasMoreElements()){
          String paramName = (String)params.nextElement();
          logger.info("Attribute: '"+paramName+"', Value: '"+req.getParameter(paramName) + "'");
      }
  }
  </code></pre>