springboot之@RequestBody踩坑小结

一开始:ajax前台:

            $.ajax({
                url:serverAddress+"system/submitSystem",
                data:{syste,"1",createUser:"2"},
                type:"POST",
                contentType:"application/json;charset=utf-8",
                beforeSend:function(){
                    i=showLoad();
                },
                success:function(result){    
                    closeLoad(i);
                    if(result.code = 400){
                        layer.msg(result.message);
                    }else if(result.code = 500){
                        layer.msg(result.message);
                    }
                    if(result.success){
                        layer.closeAll();
                        layer.msg(result.msg);
                    }
                },error:function(result){
                    closeLoad(i);
                    layer.msg(result.msg);
                }
            })

后台报错:

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

然后修改:

            $.ajax({
                url:serverAddress+"system/submitSystem",
                data:{system:data.field.system,createUser:"19120019"},
                type:"POST",
                contentType:"application/json;charset=utf-8",
                beforeSend:function(){
                    i=showLoad();
                },
                success:function(result){    
                    closeLoad(i);
                    if(result.code = 400){
                        layer.msg(result.message);
                    }else if(result.code = 500){
                        layer.msg(result.message);
                    }
                    if(result.success){
                        layer.closeAll();
                        layer.msg(result.msg);
                    }
                },error:function(result){
                    closeLoad(i);
                    console.log(JSON.stringify(result));
                    layer.msg(result.msg);
                }
            })

 

然后继续报错:

Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'system': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')

 

继续修改:

            var jsonData = {
                system:"1",
                createUser:"1"
            }
            $.ajax({
                url:serverAddress+"system/submitSystem",
                data:JSON.stringify(jsonData),
                type:"POST",
                contentType:"application/json;charset=utf-8",
                beforeSend:function(){
                    i=showLoad();
                },
                success:function(result){    
                    closeLoad(i);
                    if(result.code = 400){
                        layer.msg(result.message);
                    }else if(result.code = 500){
                        layer.msg(result.message);
                    }
                    if(result.success){
                        layer.closeAll();
                        layer.msg(result.msg);
                    }
                },error:function(result){
                    closeLoad(i);
                    console.log(JSON.stringify(result));
                    layer.msg(result.msg);
                }
            })

终于修改成功了!!!

总结:后端使用

@RequestBody

那么,后端只接收Json对象的字符串,不能接收Json对象,,用 JSON.stringify(data)的方式将对象变成字符串,同时ajax请求也要指定dataType: "json",contentType:"application/json" 。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值