Spring Boot中JSON参数传递,后台实体接受问题

1、json参数为json字符串

var data = {
    name: '超管不是11',
    password: '123456'
};
$.ajax({
    type:"post",
    url: url,
    data: JSON.stringify(data),//这里格式为JSON字符串
    dataType: 'json',//返回数据类型
    contentType:"application/json;charset=utf-8",//这里注明传递参数类型为JSON字符串
    success: function (rs) {
        console.log(rs);
    }
});
@RequestMapping(value = "/login")
//后台需要加上@RequestBody 
public Result login(@RequestBody User user){
    System.out.println(user);
    return ResultUtil.success(ResultEnum.SUCCESS, null);
}

2、JSON参数为JSON对象

var data = {
    name: '超管不是11',
    password: '123456'
};
$.ajax({
    type:"post",
    url: url,
    data: {
        name: '超管不是11',//或者 "name": "xxx"
        password: '123456'//     "password": "xxx"
    },//这里格式为JSON对象
    dataType: 'json',//返回数据类型
    //contentType:"application/json;charset=utf-8",//这里需要注释掉
    success: function (rs) {
        console.log(rs);
    }
});
@RequestMapping(value = "/login")
//public Result login(@RequestBody User user){
//    System.out.println(user);
//    return ResultUtil.success(ResultEnum.SUCCESS, null);
//}
//后台需要去掉@RequestBody, 必须去掉
public Result login(User user){
    System.out.println(user);
    return ResultUtil.success(ResultEnum.SUCCESS, null);
}

 

posted @ 2019-03-15 11:10  拂衣志  阅读(4887)  评论(0编辑  收藏  举报