//get请求获取参数 public String getParamters(HttpServletRequest request) { Map<String, String> map = Maps.newHashMap(); Enumeration<String> parameterNames = request.getParameterNames(); while (parameterNames.hasMoreElements()) { String key = parameterNames.nextElement(); String value = request.getParameter(key); map.put(key, value); } LinkedHashMap attribute = (LinkedHashMap) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); Set<Map.Entry<String, String>> entrySet = attribute.entrySet(); for (Map.Entry<String, String> entry : entrySet) { map.put(entry.getKey(), entry.getValue()); } return map.toString(); } //post请求获取参数 public String getBodyString(HttpServletRequest request) { StringBuilder sb = new StringBuilder(); InputStream inputStream = null; BufferedReader reader = null; try { inputStream = request.getInputStream(); reader = new BufferedReader( new InputStreamReader(inputStream, Charset.forName(Constants.CHAREST_CODE))); char[] bodyCharBuffer = new char[1024]; int len = 0; while ((len = reader.read(bodyCharBuffer)) != -1) { sb.append(new String(bodyCharBuffer, 0, len)); } } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return sb.toString(); }
if ("GET".equals(method)) { paramSb.append(getParamters(request)); } else if ("POST".equals(method)) { paramSb.append(getBodyString(request)); } else if ("PUT".equals(method)) { paramSb.append(getBodyString(request)); } else if ("DELETE".equals(method)) { paramSb.append(getParamters(request)); }