@RequestParam和@RequestBody的使用和区别&&使用@RequestParam和不使用@RequestParam的区别

RequestParam的使用

@RequestParam主要用于将请求参数区域的数据映射到控制层方法的参数上
首先我们需要知道@RequestParam注解主要有哪些参数

  1. value:请求中传入参数的名称,如果不设置后台接口的value值,则会默认为该变量名。比如上图中第一个参数如果不设置value=“page”,则前端传入的参数名必须为pageNum,否则在后台接口中pageNum将接收不到对应的数据

  2. required:该参数是否为必传项。默认是true,表示请求中一定要传入对应的参数,否则会报404错误,如果设置为false时,当请求中没有此参数,将会默认为null,而对于基本数据类型的变量,则必须有值,这时会抛出空指针异常。如果允许空值,则接口中变量需要使用包装类来声明。

  3. defaultValue:参数的默认值,如果请求中没有同名的参数时,该变量默认为此值。注意默认值可以使用SpEL表达式,如"#{systemProperties[‘java.vm.version’]}"

使用@RequestParm用于绑定controller上的参数,可以是多个参数,也可以是一个Map集合,GET,POST均可

@RequestParm中name属性是指定参数名,required属性默认为ture,表示必传。若为false则为非必传。属性有defaultValue默认值选项,若该参数为null时,会将默认值填充到参数上。

要求

@RequestParam的要求
1. 均支持POST,GET请求
2. 只支持Content-Type: 为 application/x-www-form-urlencoded编码的内容。Http协议中,如果不指定Content-Type,则默认传递的参数就是application/x-www-form-urlencoded类型)
3.参数列表中的参数名称是大小写敏感的

在Spring中,如果在方法参数列表中使用@RequestParam标注多个参数,会让映射方法的可读性大大降低。

如果映射请求的参数只有一两个的话,使用@RequestParam会非常直观,但是如果参数列表越来越长,就很容易晕菜。

虽然我们不能直接在参数对象中使用@RequestParam标签,但是并不代表没有其他的办法。这篇文章就会演示怎么使用对象的封装来简化多个@RequestParams标签。

【注:SpringMVC注入请求参数到对象中,这个对于很多开发是再正常不过的,但是这里强调的是使用@RequestParam来绑定参数,因为@RequestParam可以对绑定参数有更多的限制】

场景一:

如果在请求中传入多个同名参数,比如:url?userName=zhl&userName=holley时怎么办?

其实此时传入的数据格式是:“zhl,holley”,即多个数据之间使用逗号分隔开,在后台接口中可以使用数组或者list类型的变量来接收:

public String requestparam8(@RequestParam(value=“userName”) String [] userNames)

或者

public String requestparam8(@RequestParam(value=“list”) List list)

场景二:

过长的@RequestParam列表
不管是controller还是其他的类,过长的参数列表会让代码的可读性变差,这一点是所有开发人员都认同的。更不要说,如果大量的参数的类型还是一致的情况下,参数就更容易混淆了。

很多代码检查工具,都会把方法的参数个数作为检查条件,也是因为过长的参数列表被认为是一种错误的代码规范。

常见的一种解决方案,就是把一组参数合并起来,并作为应用的独立的一层。常见的,这组参数可以合并到一个对象中,并给予这个对象一个恰当的名字即可。

我们来看一个GET请求服务端的例子:

@RestController
@RequestMapping("/products")
class ProductController {
   
   //...
   @GetMapping
   List<Product> searchProducts(@RequestParam String query,
                                @RequestParam(required = false, defaultValue = "0") int offset,
                                @RequestParam(required = false, defaultValue = "10") int limit) {
   
       return productRepository.search(query, offset, limit);
   }
}

虽然该方法只有三个参数,但是参数列表很容易增长的,比如既然代码中是查询商品服务,那么常常需要包含按照一些额外的过滤条件进行排序等操作。在我们的代码中,因为参数是直接传递给数据连接层,所以我们可以直接使用ParameterObject模式来处理【注:ParameterObject就是把参数组装成对象】。

场景三:

使用@RequestParam绑定POJO

根据我的经验,很多开发没有替换较长的@RequestPar

  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
@RequestParam, @RequestBody, and @PathVariable are three annotations used in Spring Framework for handling HTTP requests. @RequestParam is used to extract parameters from the query string or form data in a request. It is used to bind the value of a query parameter to a method parameter in a Spring controller. For example, if a request contains a query parameter "name", we can use @RequestParam("name") annotation to bind the value of "name" parameter to a method parameter. Example: @GetMapping("/user") public String getUser(@RequestParam("userId") String userId) { // code to get user details using userId parameter } @RequestBody is used to extract the request body and bind it to a method parameter in a Spring controller. It is used when the request payload is in JSON or XML format. For example, if a request contains a JSON payload with user details, we can use @RequestBody annotation to map the JSON payload to a User object. Example: @PostMapping("/user") public String createUser(@RequestBody User user) { // code to create a new user using user object } @PathVariable is used to extract a variable value from the URL path and bind it to a method parameter in a Spring controller. It is used when a variable value is included in the URL path. For example, if the URL path is "/user/{userId}", we can use @PathVariable("userId") annotation to bind the value of "userId" variable to a method parameter. Example: @GetMapping("/user/{userId}") public String getUserDetails(@PathVariable("userId") String userId) { // code to get user details using userId parameter }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值