spring mvc json

来源:互联网 发布:淘宝店铺怎么加客服 编辑:程序博客网 时间:2024/06/07 23:06
 接收JSON
使用 @RequestBody 注解前台只需要向 Controller 提交一段符合格式的 JSON,Spring 会自动将其拼装成 bean。
1)在上面的项目中使用第一种方式处理返回JSON的基础上,增加如下方法:
Java代码

    @RequestMapping(value="/add",method=RequestMethod.POST, headers = {"content-type=application/json","content-type=application/xml"}) 
    @ResponseBody 
    public Object addUser(@RequestBody User user) 
    { 
        System.out.println(user.getName() + " " + user.getAge()); 
        return new HashMap<String, String>().put("success", "true"); 
    } 
0 0
原创粉丝点击