springMvc接收json和返回json对象

导入三个包

页面:

function sendJson(){
    //请求json响应json
    $.ajax({
        type:"post",
        url: "${pageContext.request.contextPath }/upload/testJson",
        contentType:"application/json;charset=utf-8",
        data:'{"username":"张三", "password":"123456"}',
        success:function(data){
            alert(data);
        }
    });
}

controller

/**
 * 1) @RequestBody User user ,将json字符串解析为User对象
 * 2) @ResponseBody 将User对象格式化为Json对象,响应
 * @param user
 * @return
 */
@RequestMapping(value="/testJson")
@ResponseBody
public User testJson(@RequestBody User user){
     user.setGender("男");
     user.setSalary(32.36);   
     return user;
}

 

posted @ 2018-06-20 19:04  Aaron2705  阅读(201)  评论(0编辑  收藏  举报