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串了
方式一:通过流的方方式
//在controll中进行调用
String content = HttpJsonUtils.getPostByApplicationForm(request);
LOGGER.info("content*************" + content);
JSONObject jsObject = JSONObject.fromObject(content);
try {
ipbegin = jsObject.getLong("ipbegin");
ipend = jsObject.getLong("ipend");
province = jsObject.getString("province");
isopen = jsObject.getString("isopen");
opertime = jsObject.getString("opertime");
sign = jsObject.getString("sign");
} catch (Exception e) {
// TODO: handle exception
e.getMessage();
LOGGER.info("发生错误*****" + e.getMessage());
}
LOGGER.info("ipbegin********************************" + ipbegin);
LOGGER.info("ipend********************************" + ipend);
LOGGER.info("province********************************" + province);
LOGGER.info("isopen********************************" + isopen);
LOGGER.info("opertime********************************" + opertime);
LOGGER.info("sign********************************" + sign);
方式二:通过获取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://www.cnblogs.com/SimonHu1993/p/7295765.html
(转)获取 request 中用POST方式"Content-type"是"application/x-www-form-urlencoded;charset=utf-8"发送的 json 数据
原文:http://www.cnblogs.com/SimonHu1993/p/7295750.html