不一样的传值方式
- 首先咱们大多数的前端传值是拿实体(entity)来接收的,今天我要说的是另一种传值接收的方式,这种方式适合和数据库没有交互的时候使用还有就是在传实体的时候,需要把实体内的值进行加密而不是整体加密的时候
- 咱们一般接收值使用的方法
@RequestMapping(value="/sysnc/heartbeat",method=RequestMethod.POST,produces = "application/json;charset=utf-8")
@ResponseBody
public JSONObject sysnc_heartbeat(Teachar teachar) {
............................
}
@RequestMapping(value="/sysnc/heartbeat",method=RequestMethod.POST,produces = "application/json;charset=utf-8")
@ResponseBody
public JSONObject sysnc_heartbeat(String condition) {
....................
}
- 大家可能很好奇String应该怎么解析呢?
- 首先传过来的依旧是Json传 只不过是一个字符串,然后我们会通过一系列的方法把String化的Json串进行处理,代码如下:
JSONObject jsonObjectO= (JSONObject) JSON.parse(condition);
System.out.println(jsonObjectO.toString());
String con1=jsonObjectO.getString("head");
JSONObject jsonObjectS= (JSONObject) JSON.parse(keyword);
- 这样既处理好一个复杂的String化Json(如{“heads”:{“token”:“xxxxx”,“version”:“xxxxx”},“body”:{“msg”:“xxxxxx”,“code”:“xxxxx”}})
处理好后我们从jsonObjectS中取出自己想要的值。
jsonObjectS.getString("fromtype");
- 大家肯定很好奇{“heads”:{“token”:“xxxxx”,“version”:“xxxxx”},“body”:{“msg”:“xxxxxx”,“code”:“xxxxx”}}这种Json怎么写是最简单的,我想大家最常用的肯定就是字符串拼接,我最近知道了另一种巨简便的方式代码如下
Map<String,Object> headMap=new HashMap<String,Object>();
headMap.put("version","1.0");
headMap.put("fromtype","143242453124234");
headMap.put("sessionid","234235234234234234324324325324");
headMap.put("time",time);
headMap.put("token",MD5.getMD5ofStr("123456143242453124234"));
Map<String,Object> bodyMap=new HashMap<String,Object>();
bodyMap.put("times", dates);
bodyMap.put("msg", "成功");
bodyMap.put("code", "1");
Map<String,Object> paraMap=new HashMap<String,Object>();
paraMap.put("head",headJson);
paraMap.put("body",bodyJson);
JSONObject Reslut = new JSONObject(paraMap);