SpringMvc中关于@ResponseBody和HashMap的用法

由于毕业设计用到的LayUI这个框架,在使用到它的表格控件的时候,需要返回它规定格式的数据,他默认的格式是这样的:

{
  code: 0,
  msg: "",
  count: 1000,
  data: []
} 

data即为查询出来的数据,前两个参数是必须的,而使用@ResponseBody则能将对象转换成对应的Json字符串,第一次使用的时候,不知道怎么把前两个参数加上,后来在网上查了一下,看到一些大佬是这么用的:

 @RequestMapping(value = "/computerList", method = RequestMethod.GET)
    public @ResponseBody
    HashMap print(HttpServletRequest request, HttpServletResponse response) {
        HashMap map = new HashMap();
        Computer computer = new Computer();
        List<Computer> computerList = computerService.selectAllComputers(computer);
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", computerList);
        return map;
    }
返回的数据格式是:

{"msg":"","code":0,"data":[{"cptId":1,"cptName":"华硕A550jk4200","cptPrice":4500.00,"cptIcon":null,"cptOs":"win8","cptCpu":"intel core i5_4200H","cptRam":"4G","cptRom":"1T","cptScreensize":"15.4","cptGraphicscard":"GT840M","cptIslending":"N","cptLendtime":null,"cptReturntime":null,"cptWho":null,"attribute1":null,"attribute2":null,"attribute3":null}]}
后来我就学到了这招,用上了layui这个前端框架,减少了很多的工作量 偷笑

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值