当前位置:   article > 正文

SpringMvc,全面讲解@RequestParam注解的用法和原理

@requestparam

本文要讲的@RequestParam注解大家在开发中应该会经常的用到,但是它的某些用法我感觉你不一定都知道,所以这篇文章就讲解一下带大家拨开云雾全面了解这个注解,使大家在开发中使用到这个注解的时候不再一知半解。

先看一下@RequestParam这个注解的源码:

  1. @Target(ElementType.PARAMETER)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. public @interface RequestParam {
  5. /**
  6. * 对应request中参数名称
  7. */
  8. @AliasFor("name")
  9. String value() default "";
  10. /**
  11. * 同value
  12. */
  13. @AliasFor("value")
  14. String name() default "";
  15. /**
  16. * 请求中是否必须有这个参数,默认为true,
  17. * 如果使用默认的true,前台没有传递这个参数会报错。
  18. */
  19. boolean required() default true;
  20. }

好了下面举几个例子带大家去全面了解这个注解的用法。

1:@RequestParam 指定 name,获取对应参数的值。第一个例子应该就有你不知道的用法,仔细看。

  1. /**
  2. * 中指定name,用来取name的值对应的请求参数中的值
  3. * @param name:可以不传递name参数,不传递的时候默认值为ready
  4. * @param age
  5. * @param pets
  6. * @return
  7. */
  8. @RequestMapping("/requestparam/test1")
  9. public Map<String, Object> test1(
  10. @RequestParam(value = "name", required = false, defaultValue = "ready") String name,
  11. //相当于request.getParameter("name")
  12. @RequestParam("age") int age,
  13. //Integer.parseInt(request.getParameter("age"))
  14. @RequestParam("interests") String[] interests,
  15. //request.getParameterValues("pets")
  16. // 这个解释以下,如果你传递多个pets,会自动给你分装成一个list,这个用法还是挺牛的。
  17. @RequestParam("pets") List<String> pets
  18. //Arrays.asList(request.getParameterValues("pets"))
  19. ) {
  20. Map<String, Object> result = new LinkedHashMap<>();
  21. result.put("name", name);
  22. result.put("age", age);
  23. result.put("interests", interests);
  24. result.put("pets", pets);
  25. return result;
  26. }

我们使用postman测试一下:

上面传递的参数interests,pets传递多个值,参数使用集合或者数组接收@RequestParam会为你自动转换成数组或者集合。

2:required 属性为 true,不传,则报错如下age不传值就报错了,但是当你把require设置为false的时候就访问正常了:

3:@RequestParam 不指定 name,获取所有参数值,这个开发中不建议大家这样使用,因为你不知道有几个具体参数值,但是某些功能的开发会使用到,要了解一下。

当我们想用一个 Map 来接收所有参数的之后,代码如下,@RequestParam 不用指定 name 的值,参数类型为 Map<String,String>,所有请求参数会以参数名称:值的方式丢在 Map 中。代码如下:

  1. /**
  2. * 不指定name,用于接收所有参数的值,
  3. * 参数类型为Map<String,String>,key为请求中的参数名称,
  4. * value为值
  5. * @param paramMap
  6. * @return
  7. */
  8. @RequestMapping("/requestparam/test2")
  9. public Map<String, String> test2(@RequestParam Map<String, String> paramMap) {
  10. return paramMap;
  11. }

 我们使用postman调用一下如下图:

当你直接使用map接收的时候,Interests或者pets只会取我们传递的第一个值,这样在开发中就有问题了,我们明明要给后台传输多个值,但是没收到。这个时候你就想了这是严重的bug啊,开发中万一我们一定要使用map接收呢?别慌还有下面使用方法的。

3.2:@RequestParam 不指定 name,获取所有参数值。这个接口的参数是MultiValueMap类型。代码如下:

  1. /**
  2. * 不指定name,用于接收所有参数的值,
  3. * 参数类型为MultiValueMap<String, String>:
  4. * key为请求中的参数名称,value为值的集合List<String>
  5. * @param paramMap
  6. * @return
  7. */
  8. @RequestMapping(value = "/requestparam/test3", produces = MediaType.APPLICATION_JSON_VALUE)
  9. public MultiValueMap<String, String> test3(@RequestParam MultiValueMap<String, String> paramMap) {
  10. return paramMap;
  11. }

 使用postman调用一下如下图:

 参数是MultiValueMap类型,这玩意是干啥的?好像很陌生啊,哈哈再陌生的东西,把其源码放出来,瞬间明了了,如下代码,可以看出来MultiValueMap相当于Map<String,List<String>>。大家可以记住这种数据结构,开发中遇到这种数据结构的功能,直接使用MultiValueMap十分的好用。

  1. public interface MultiValueMap<K, V> extends Map<K, List<V>> {
  2. /**
  3. * Return the first value for the given key.
  4. * @param key the key
  5. * @return the first value for the specified key, or {@code null} if none
  6. */
  7. @Nullable
  8. V getFirst(K key)

好了总结一下@RequestParam这个注解的用法:

1:@RequestParam 注解用来标注在控制器方法的参数上,springmvc 从 request 中获取请求的值赋值给方法的参数。

2:@RequestParam 指定 name 时,可以获取 request 中指定参数的值,相当于 request.getParameter(name)或 request.getParameters(name)。

3:@RequestParam 未指定 name,参数类型为:Map<String,String>时,用来接收 request 中所有参数的值,Map 中 key 为参数名称,value 为参数的值。

4:@RequestParam 未指定 name,参数类型为 MultiValueMap<String, String>时,用来接收 request 中所有参数的值,key 为请求中的参数名称,value 为值的集合 List<String>。

看过上面几个例子你应该完全了解了@RequestParam的用法了吧。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/310164
推荐阅读
相关标签
  

闽ICP备14008679号