request中發送json數據用post方式發送Content-type用application/json;charset=utf-8方式發送的話,直接用springMVC的@RequestBody標簽接收后面跟實體對象就行了,spring會幫你自動拼裝成對象,如果Content-type設置成application/x-www-form-urlencoded;charset=utf-8就不能用spring的東西了,只能以常規的方式獲取json串了
方式一:通過流的方方式
import java.io.IOException; import javax.servlet.http.HttpServletRequest; /** * request 對象的相關操作 * @author zhangtengda * @version 1.0 * @created 2015年5月2日 下午8:25:43 */ public class GetRequestJsonUtils { /*** * 獲取 request 中 json 字符串的內容 * * @param request * @return : <code>byte[]</code> * @throws IOException */ public static String getRequestJsonString(HttpServletRequest request) throws IOException { String submitMehtod = request.getMethod(); // GET if (submitMehtod.equals("GET")) { return new String(request.getQueryString().getBytes("iso-8859-1"),"utf-8").replaceAll("%22", "\""); // POST } else { return getRequestPostStr(request); } } /** * 描述:獲取 post 請求的 byte[] 數組 * <pre> * 舉例: * </pre> * @param request * @return * @throws IOException */ public static byte[] getRequestPostBytes(HttpServletRequest request) throws IOException { int contentLength = request.getContentLength(); if(contentLength<0){ return null; } byte buffer[] = new byte[contentLength]; for (int i = 0; i < contentLength;) { int readlen = request.getInputStream().read(buffer, i, contentLength - i); if (readlen == -1) { break; } i += readlen; } return buffer; } /** * 描述:獲取 post 請求內容 * <pre> * 舉例: * </pre> * @param request * @return * @throws IOException */ public static String getRequestPostStr(HttpServletRequest request) throws IOException { byte buffer[] = getRequestPostBytes(request); String charEncoding = request.getCharacterEncoding(); if (charEncoding == null) { charEncoding = "UTF-8"; } return new String(buffer, charEncoding); }
方式二:通過獲取Map的方式處理
這種剛方式存在弊端,如果json數據中存在=號,數據會在等號的地方斷掉,后面的數據會被存儲成map的values,需要重新拼裝key和values的值,拼裝成原來的json串
/** * 方法說明 :通過獲取map的方式 */ @SuppressWarnings("rawtypes") private String getParameterMap(HttpServletRequest request) { Map map = request.getParameterMap(); String text = ""; if (map != null) { Set set = map.entrySet(); Iterator iterator = set.iterator(); while (iterator.hasNext()) { Map.Entry entry = (Entry) iterator.next(); if (entry.getValue() instanceof String[]) { logger.info("==A==entry的key: " + entry.getKey()); String key = (String) entry.getKey(); if (key != null && !"id".equals(key) && key.startsWith("[") && key.endsWith("]")) { text = (String) entry.getKey(); break; } String[] values = (String[]) entry.getValue(); for (int i = 0; i < values.length; i++) { logger.info("==B==entry的value: " + values[i]); key += "="+values[i]; } if (key.startsWith("[") && key.endsWith("]")) { text = (String) entry.getKey(); break; } } else if (entry.getValue() instanceof String) { logger.info("==========entry的key: " + entry.getKey()); logger.info("==========entry的value: " + entry.getValue()); } } } return text; }
方式三:通過獲取所有參數名的方式
這種方式也存在弊端 對json串中不能傳特殊字符,比如/=, \=, /, ~等的這樣的符號都不能有如果存在也不會讀出來,他的模式和Map的方式是差不多的,也是轉成Map處理的
/** * 方法說明 :通過獲取所有參數名的方式 */ @SuppressWarnings({ "rawtypes", "unchecked" }) private String getParamNames(HttpServletRequest request) { Map map = new HashMap(); Enumeration paramNames = request.getParameterNames(); while (paramNames.hasMoreElements()) { String paramName = (String) paramNames.nextElement(); String[] paramValues = request.getParameterValues(paramName); if (paramValues.length == 1) { String paramValue = paramValues[0]; if (paramValue.length() != 0) { map.put(paramName, paramValue); } } } Set<Map.Entry<String, String>> set = map.entrySet(); String text = ""; for (Map.Entry entry : set) { logger.info(entry.getKey() + ":" + entry.getValue()); text += entry.getKey() + ":" + entry.getValue(); logger.info("text------->"+text); } if(text.startsWith("[") && text.endsWith("]")){ return text; } return ""; }
附上一點常用的Content-type的方式
application/x-javascript text/xml->xml數據 application/x-javascript->json對象 application/x-www-form-urlencoded->表單數據 application/json;charset=utf-8 -> json數據
最后附上發送方式的連接
http://blog.csdn.net/mingtianhaiyouwo/article/details/51381853
request中發送json數據用post方式發送Content-type用application/json;charset=utf-8方式發送的話,直接用springMVC的@RequestBody標簽接收后面跟實體對象就行了,spring會幫你自動拼裝成對象,如果Content-type設置成application/x-www-form-urlencoded;charset=utf-8就不能用spring的東西了,只能以常規的方式獲取json串了
方式一:通過流的方方式
方式二:通過獲取Map的方式處理
這種剛方式存在弊端,如果json數據中存在=號,數據會在等號的地方斷掉,后面的數據會被存儲成map的values,需要重新拼裝key和values的值,拼裝成原來的json串
方式三:通過獲取所有參數名的方式
這種方式也存在弊端 對json串中不能傳特殊字符,比如/=, \=, /, ~等的這樣的符號都不能有如果存在也不會讀出來,他的模式和Map的方式是差不多的,也是轉成Map處理的
附上一點常用的Content-type的方式
application/x-javascript text/xml->xml數據 application/x-javascript->json對象 application/x-www-form-urlencoded->表單數據 application/json;charset=utf-8 -> json數據
最后附上發送方式的連接
http://blog.csdn.net/mingtianhaiyouwo/article/details/51381853