@RequestBody与@RequestParam的使用以及ajax、postman、restfultool的JSON发送格式问题

引言

  在数据测试的过程中发现,同一个Controller同一个Mapping,使用前端页面发送ajax请求成功接收到数据,使用postman的x-www-form-urlencoded发送请求成功接收到数据,但是使用postman的raw发送请求的和restfultool发送JSON格式的请求都失败了(传入的对象为空)。

        在参数前加上注解@RequestBody(@RequestBody User user)postman-rawrestfultool工具发送请求成功了,但是ajaxpostman的x-www-form-urlencoded却又失败了。

        本文将解释记录说明@RequestBody、@RequestParam注解与ajax、postman、restfultool发送的请求的成功与失败的原因,以及请求的发送和@RequestBody、@RequestParam两个注解的使用。

controller:

 @PutMapping
    public ResultData updateUser(User user){
        System.out.println("要更新的对象是"+user);
        return new ResultData(888,"成功",userService.updateById(user));
    }

前端ajax:成功

var user = new Object();
        user.username = $("#username").val();
        user.password = $("#password").val();
        user.id = $("#uid").val();
        user._method = "put";
        $.ajax({
            url:"/users",
            data:user,
            dataType:"json",
            type:"post",
            success:function (res){
                if (res.code==888){
                    alert("修改成功!");
                    location.href = "/html/user_list.html";
                }else {
                    alert("修改失败!");
                }
            }

        });

Postman——www-form-urlencoded:成功

Postman——raw JSON:失败

 RestfulTool:失败

原因分析与解决办法

1.前端请求传Json对象则后端使用@RequestParam

2.前端请求传Json对象的字符串则后端使用@RequestBody

postman-rawrestfultool发送的都是json对象需要使用@RequestBody

postman-www-form-urlencoded发送的是json字符串需要使用@RequestParam(参数名一样可以不加)

使用ajax两种都可以发送

ajax发送json对象:后端使用@RequestParam(参数名一样可以不加)

var user = new Object();
        user.username = $("#username").val();
        user.password = $("#password").val();
        user.id = $("#uid").val();
        user._method = "put";
        $.ajax({
            url:"/users",
            data:user,
            dataType:"json",
            type:"post",
            success:function (res){
                if (res.code==888){
                    alert("修改成功!");
                    location.href = "/html/user_list.html";
                }else {
                    alert("修改失败!");
                }
            }

        });

ajax发送json字符串:后端使用@RequestBody

        var user = new Object();
        user.username = $("#username").val();
        user.password = $("#password").val();
        user.id = $("#uid").val();
        // user._method = "put";
        $.ajax({
            url:"/users",
            data:JSON.stringify(user),
            dataType:"json",
            contentType: "application/json;charset=utf-8",
            type:"put",
            success:function (res){
                if (res.code==888){
                    alert("修改成功!");
                    location.href = "/html/user_list.html";
                }else {
                    alert("修改失败!");
                }
            }

        });

        如果发送的json和应该使用的注解不一致(比如前端发送JSON对象,后端却使用@RequestBody、前端发送JSON字符串,后端使用@RequestParam)可能会抛出异常Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

问题点1:

如果Content-Type设置为“application/x-www-form-urlencoded;charset=UTF-8”无论是POST请求还是GET请求都是可以通过这种方式成功获取参数,但是如果前端POST请求中的body是Json对象的话,会报上述错误。

请求中传JSON时设置的Content-Type 如果是application/json或者text/json时,JAVA中request.getParameter("")怎么也接收不到数据。这是因为,Tomcat的HttpServletRequest类的实现类为org.apache.catalina.connector.Request(实际上是org.apache.coyote.Request)。

问题点2:

当前端请求的Content-Type是Json时,可以用@RequestBody这个注解来解决。@RequestParam 底层是通过request.getParameter方式获得参数的,换句话说,@RequestParam 和request.getParameter是同一回事。因为使用request.getParameter()方式获取参数,可以处理get 方式中queryString的值,也可以处理post方式中 body data的值。所以,@RequestParam可以处理get 方式中queryString的值,也可以处理post方式中 body data的值。@RequestParam用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容,提交方式GET、POST。

@RequestBody接受的是一个json对象的字符串,而不是Json对象,在请求时往往都是Json对象,用JSON.stringify(data)的方式就能将对象变成json字符串。

 参考:

chttps://www.cnblogs.com/shirandedan/p/7727326.html

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值