@RequestBody 和 @RequestParam可以同时使用

@RequestParam和@RequestBody这两个注解是可以同时使用的。

网上有很多博客说@RequestParam 和@RequestBody不能同时使用,这是错误的。根据HTTP协议,并没有说post请求不能带URL参数,经验证往一个带有参数的URL发送post请求也是可以成功的。只不过,我们日常开发使用GET请求搭配@RequestParam,使用POST请求搭配@RequestBody就满足了需求,基本不怎么同时使用二者而已。

         自己个人实际验证结果:

package com.example.model;

import java.util.List;

public class PramInfo {
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    private int id;
    private String str;

}

package com.example.demo;

import com.example.model.PramInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping(value = "/test")
public class TestContoller {

    @RequestMapping(value = "/dxc")
    public String print(PramInfo info) {
        return info.getId() + ": :" + info.getStr();
    }

    @RequestMapping(value = "/getUserJson")
    public String getUserJson(@RequestParam(value = "id") int id, @RequestParam(value = "name2", required = false) String name2
            , @RequestBody PramInfo pramInfo) {
        return (id + "--" + name2 + ";paramInfo:" + pramInfo.getStr() + ";pramInfo.id:" + pramInfo.getId());
    }
}

 
在postman发送如下post请求,返回正常:
 
body中参数如下:
 
        从结果来看,post请求URL带参数是没有问题的,所以@RequestParam和@RequestBody是可以同时使用的【经测试,分别使用Postman 和 httpClient框架编程发送http请求,后端@RequestParam和@RequestBody都可以正常接收请求参数,所以个人认为可能一些前端框架不支持或者没必要这么做,但是不能说@RequestParam和@RequestBody 不能同时使用】。
值得注意的地方:
1、postman的GET请求是不支持请求body的;
2、
 
 @GetMapping(value = "/dxc")
    public String print(PramInfo info) {
        return info.getId() + ": :" + info.getStr();
    }
这种请求方式,不加@RequestParam注解,也能直接取出URL后面的参数,即参数可以与定义的类互相自动转化。
 
 
3、
@PostMapping(value = "/getUserJson")
    public String getUserJson(@RequestParam(value = "id") int id, @RequestParam(value = "name2", required = false) String name2
            , @RequestBody PramInfo pramInfo) {
        return (id + "--" + name2 + ";paramInfo:" + pramInfo.getStr() + ";pramInfo.id:" + pramInfo.getId());
    

 

如果请求中的body没有ParamInfo对象对应的json串,即当body为空(连json串的{}都没有)时,会报请求body空异常:
 
 
 
2018-05-12 23:45:27.494  WARN 6768 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.lang.String com.example.demo.TestContoller.getUserJson(int,java.lang.String,com.example.model.PramInfo)-05-12 23:45:27.494  WARN 6768 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.lang.String com.example.demo.TestContoller.getUserJson(int,java.lang.String,com.example.model.PramInfo)
  • 22
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 24
    评论
@RequestBody和@RequestParam是Spring MVC中两个常用的注解,用于处理请求参数。 @RequestBody注解用于指定请求体的参数,通常用于接收Json格式的请求参数。它会将请求体中的数据反序列化为Java对象,并绑定到方法的参数上。这个注解在处理POST请求时常用。 @RequestParam注解用于指定请求参数的参数,通常用于接收URL参数或表单参数。它会从请求中获取指定名称的参数值,并绑定到方法的参数上。这个注解在处理GET请求时常用。 所以,@RequestBody适用于接收复杂的请求数据,而@RequestParam适用于接收简单的请求参数。 引用中的内容提供了关于这两个注解的详细说明。@RequestParam注解拥有value和name两个属性,用于指定参数的名称。它还有一个required属性,用于指定参数是否必选,默认为true。@RequestBody注解只有一个required属性,默认为true,表示请求体中的参数必须传递。 引用中的内容提供了对这两个注解的更加详细的解释和使用示例。 需要注意的是,GET请求通常不适合使用@RequestBody注解,因为GET请求的参数会附加在URL上,有长度限制。而@RequestBody适用于处理POST请求中的请求体参数。 希望以上回答对您有所帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [@RequestBody @RequestParam使用介绍](https://blog.csdn.net/zzhuan_1/article/details/102458004)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [@RequestParam和@RequestBody使用](https://blog.csdn.net/lijingjingchn/article/details/116166442)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值