我们有一个Spring方法来处理REST调用,根据我们使用的客户端(浏览器与移动应用程序),我们遇到了一些问题。我们希望能够看到原始请求和标题,但我们无法弄清楚如何轻松地做到这一点。我们提出的最好的方法是将HttpServletRequest
添加到方法中的参数中,并创建一个long方法来打印请求对象的各个部分。是否有更好的方法可以打开某些特定org.springframework.web.*
Spring类的调试日志记录?
我们的方法的编辑版本和printRequestInfo()
方法是:
@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) + "'");
}
}
答案 0 :(得分:1)
您提出的解决方案或多或少是使用Servlet API(Spring MVC构建在其上的最佳方式),因此它不会提供任何 more 。
根据您使用的Servlet容器,它们可能已经具有您可以使用的实现。例如,Tomcat提供org.apache.catalina.filters.RequestDumperFilter
请求和响应信息。
答案 1 :(得分:0)
我找不到其他方法。
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;
}