能问这种问题,大家应该知道本人是新手,有经验的高手就是弹指之间的事情,所以请大家多多指点。
例如在client端本人要发送一个json字符串过来,在controller里面怎么样将这个json字符串自动转化为java对象 ?
前端app发送这个json数据包::
例如在client端本人要发送一个json字符串过来,在controller里面怎么样将这个json字符串自动转化为java对象 ?
前端app发送这个json数据包::
{"action":"poststaff", "data":[{ "action":"1", "key":{"staff_id":"123456"}, "data":{ "staff_id":"123456", "age":"18", "salary_grd":"A1", "join_date":"2015-02-03" } }, { "action":"2", "key":null, "data":{ "staff_id":"123456", "age":"18", "salary_grd":"A1", "join_date":"2015-02-03" } }] }
后台对应的Class:
public class StaffTable{ public String staff_id; public int age; public String salary_grd; public Date join_date; } public class PostListBean{ public static class KeyBean{ public String staff_id; } public String action; public KeyBean key; public StaffTable data; } public class PostListActionBean{ public String action; public ArrayList<PostListBean> data; }
那么controller该怎么写才能将上述的json数据包和pvParams自动映射到一起?
@Controller public class PostStaffController { @RequestMapping("/poststaff") public ModelAndView poststaff(@RequestParam(value = "??", defaultValue = "??") PostListActionBean pvParams) { System.out.println(pvParams); Map<String, Object> map = new HashMap<String, Object>(); //return new ModelAndView("/ok", map); } }
高手伞,public ModelAndView poststaff(@RequestParam(value = “??”, defaultValue = “??”) PostListActionBean pvParams)
这段应该怎么写?
解决方案
10
你的是json数组,用字符串接收,转java 数组对象。本人用的是jsckson
@RequestMapping(value = "/wabiaozai", method = RequestMethod.POST) public void myDomain(HttpServletRequest request, @RequestBody String myDomain) throws Exception{ ObjectMapper objectMapper = new ObjectMapper(); JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, MyDomain.class); List<MyDomain> list = objectMapper.readValue(myDomain, javaType); System.out.println(""); }
具体可以参照http://blog.csdn.net/wabiaozia/article/details/50803581解决方案二
10
jsp中用form表单提交,在from表单那实体类中有get,set方法后,json传入的参数name=123与实体类中的参数setName名相同,在后台就可以直接getName就获取到123了吧,json中也有data:$(“#userForm”).serialize(),来提交表单了。