6

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 HttpServletRequest 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 org.springframework.web.* Spring class?

An edited version of our method and the printRequestInfo() method is:

@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) + "'");
    }
}
Chris Williams
  • 11,647
  • 15
  • 60
  • 97
  • Register one of the subclass of the `AbstractRequestLoggingFilter` and configure it appropriately. See also http://stackoverflow.com/questions/25905296/how-to-log-httprequest-and-httpresponse-in-a-file/25906059#25906059. – M. Deinum Sep 23 '14 at 17:58

2 Answers2

3

I couldn't find any other way to do it.

private Map<String, String> getRequestInformation(HttpServletRequest request) {
    Map<String, String> map = new HashMap<String, String>();
    Enumeration headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String key = (String) headerNames.nextElement();
        String value = request.getHeader(key);
        map.put("header: " + key, value);
    }
    Enumeration parameterNames = request.getParameterNames();
    while (parameterNames.hasMoreElements()) {
        String key = (String) parameterNames.nextElement();
        String value = request.getParameter(key);
        map.put("parameter: " + key, value);
    }
    Cookie[] cookies = request.getCookies();
    for (int i = 0; i < cookies.length; i++) {
        Cookie cookie = cookies[i];
        map.put("cookie: " + cookie.getName(), cookie.getValue());

    }
    while (parameterNames.hasMoreElements()) {
        String key = (String) parameterNames.nextElement();
        String value = request.getParameter(key);
        map.put("parameter: " + key, value);
    }
    map.put("getRequestIPAdrress", getRequestIPAdrress(request));
    map.put("getRemoteUser", request.getRemoteUser());
    map.put("getMethod", request.getMethod());
    map.put("getQueryString", request.getQueryString());
    map.put("getAuthType", request.getAuthType());
    map.put("getContextPath", request.getContextPath());
    map.put("getPathInfo", request.getPathInfo());
    map.put("getPathTranslated", request.getPathTranslated());
    map.put("getRequestedSessionId", request.getRequestedSessionId());
    map.put("getRequestURI", request.getRequestURI());
    map.put("getRequestURL", request.getRequestURL().toString());
    map.put("getMethod", request.getMethod());
    map.put("getServletPath", request.getServletPath());
    map.put("getContentType", request.getContentType());
    map.put("getLocalName", request.getLocalName());
    map.put("getProtocol", request.getProtocol());
    map.put("getRemoteAddr", request.getRemoteAddr());
    map.put("getServerName", request.getServerName());
    return map;
}
1

The solution you are proposing is more or less the best you can do with the Servlet API (which Spring MVC is built on, so it doesn't offer anything more).

Depending on which Servlet container you are using, they may already have an implementation that you can use. For example, Tomcat offers org.apache.catalina.filters.RequestDumperFilter request and response information.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724