最近在做项目,用此贴记录遇到的一些小问题
1.关于json
首先需要引入3个包,我看网上有人说只需要两个,但是我没成功,引入3个之后才没问题的
关于json的返回格式,如果想增加内容的话最好用一个map集合包裹住,最近用bootstrap table做分页,要求返回的json格式如下:
也就是说要增加”total”: 792,”rows”:这些东西,后面返回的是对象集合.
{"total": 792,"rows": [{"id":1,"name":"test1","price":"$1"},{"id":2,"name":"test2","price":"$2"},{"id":3,"name":"test3","price":"$3"},{"id":4,"name":"test4","price":"$4"},{"id":5,"name":"test5","price":"$5"},{"id":6,"name":"test6","price":"$6"},{"id":7,"name":"test7","price":"$7"},{"id":8,"name":"test8","price":"$8"},{"id":9,"name":"test9","price":"$9"},{"id":10,"name":"test10","price":"$10"}]}
因此我用map包裹住
@RequestMapping(value = "/getAllUser",method = RequestMethod.POST) public @ResponseBody Map<String,Object> getAllCumUser(@RequestBody PageUtil pageUtil){ Map<String,Object> model = new HashMap<>(); //这里引入分页机制 PageHelper.startPage(pageUtil.getOffset()/pageUtil.getLimit()+1,pageUtil.getLimit()); List<CumUser> list = cumUserMapper.findAllUser(pageUtil); PageInfo<CumUser> info = new PageInfo<>(list); //这样做就可以返回上述那种格式的json了 model.put("total",info.getTotal()); model.put("rows",list); return model; }
关于接收json
可以用一个对象类来承载,在里面写上json的key这个属性值,实现set方法,spring MVC会自动注入值的