1.前端的操作:
对象接收json数据方式:将所需要传的数据转化为json数据,并将这些数据以post方式传到后台的controller层,然后controller层接收json数据,并且是以对象的形式进行接收。
这里面是springmvc自动控制将这些数据转化为对象,不需要辅助包fastjson。
用Map接收json数据:需要使用fastjson包(前提json含有对象数据)。在看github项目的学习过程中,发现:封装的对象中,可以有map类型,更加方便地接收参数,将json数据一一绑定到map映射中,方便我们对json数据操作哦。
2.代码实现:
模拟前端传过来的json数据
{"people": {"username": "Tom","password": "123456","sex": "男","address": {"province": "安徽","city": "芜湖","town": "天堑","village": "小岗村"},"phones": [
{"operator": "中国移动","phoneNumber": "12345678901"},
{"operator": "中国联通","phoneNumber": "09876543211"}
]
}
}
Address.java
importlombok.Data;
@Datapublic classAddress {privateString province;privateString city;privateString town;privateString village;
}
Phone.java
importlombok.Data;
@Datapublic classPhone {privateString operator;privateString phoneNumber;
}
People.java
importlombok.Data;importjava.util.List;
@Datapublic classPeople {privateString username;privateString password;privateString sex;privateAddress address;private Listphones;
}
controller类
importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONObject;importcom.yyj.demo.pojo.Address;importcom.yyj.demo.pojo.People;importcom.yyj.demo.pojo.Phone;importorg.springframework.web.bind.annotation.RequestBody;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.Map;
@RestController
@RequestMapping("/people")public classPersonController {
@RequestMapping("getPeople")public MapgetPeople(@RequestBody People people){if(people == null){return null;
}
Map param = new HashMap();
String s=people.getPhones().toString();
System.out.println(s);
param.put("people",people);returnparam;
}
@RequestMapping("getOtherPeople")public Map getOtherPeople(@RequestBody Mapparams){if(params == null){
System.out.println("系统接收参数错误");return null;
}
Map map = new HashMap<>();
People people= newPeople();
people.setUsername(params.get("username").toString());
people.setPassword(params.get("password").toString());
people.setSex(params.get("sex").toString());
people.setAddress(JSON.parseObject(JSONObject.toJSONString(params.get("address")),Address.class));
people.setPhones((ArrayList) params.get("phones"));
map.put("people",people);returnmap;
}
}/*{
"username": "Tom",
"password": "123456",
"sex": "男",
"address": {
"province": "安徽",
"city": "芜湖",
"town": "天堑",
"village": "小岗村"
},
"phones": [{
"operator": "中国移动",
"phoneNumber": "12345678901"
},
{
"operator": "中国联通",
"phoneNumber": "09876543211"
}
]
}*/
3.postman测试结果:
对象接收方式:
Map接收json数据
4.第二种方式需要添加alibaba的fastjson依赖
com.alibaba
fastjson
1.2.47
5.第二种实现方式遇到的各种坑:
使用@ResquestBody Map接收前端json数据,json数据中有数组,我们不能用String[]去接收这个数组,要用ArrayList去接收,原因如下:@RequsetBody会读取request的body数据,之后会调用MappingJackson2HttpMessageConverter解析数据,会使用到ObjectMapper,之后this.objectMapper.readValue(inputMessage.getBody(), javaType);javaType是AbstractJackson2HttpMessageConverter中getJavaType优选出来可以解析context的类型.
json数据中有对象数据存在时,我们不能单纯的去直接用这个对象接收这个数据,会报错的。报错内容:java.util.LinkedHashMap cannot be cast to ******我的具体做法如下:我们可以接收的hashmap转换为json,然后再次把这个json转换为对应的对象。(如有更好的办法,还望大佬在评论区告诉我一下)