本文要讲的@RequestParam注解大家在开发中应该会经常的用到,但是它的某些用法我感觉你不一定都知道,所以这篇文章就讲解一下带大家拨开云雾全面了解这个注解,使大家在开发中使用到这个注解的时候不再一知半解。
先看一下@RequestParam这个注解的源码:
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
/**
* 对应request中参数名称
*/
@AliasFor("name")
String value() default "";
/**
* 同value
*/
@AliasFor("value")
String name() default "";
/**
* 请求中是否必须有这个参数,默认为true,
* 如果使用默认的true,前台没有传递这个参数会报错。
*/
boolean required() default true;
}
好了下面举几个例子带大家去全面了解这个注解的用法。
1:@RequestParam 指定 name,获取对应参数的值。第一个例子应该就有你不知道的用法,仔细看。
/**
* 中指定name,用来取name的值对应的请求参数中的值
* @param name:可以不传递name参数,不传递的时候默认值为ready
* @param age
* @param pets
* @return
*/
@RequestMapping("/requestparam/test1")
public Map<String, Object> test1(
@RequestParam(value = "name", required = false, defaultValue = "ready") String name,
//相当于request.getParameter("name")
@RequestParam("age") int age,
//Integer.parseInt(request.getParameter("age"))
@RequestParam("interests") String[] interests,
//request.getParameterValues("pets")
// 这个解释以下,如果你传递多个pets,会自动给你分装成一个list,这个用法还是挺牛的。
@RequestParam("pets") List<String> pets
//Arrays.asList(request.getParameterValues("pets"))
) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("name", name);
result.put("age", age);
result.put("interests", interests);
result.put("pets", pets);
return result;
}
我们使用postman测试一下:
上面传递的参数interests,pets传递多个值,参数使用集合或者数组接收@RequestParam会为你自动转换成数组或者集合。
2:required 属性为 true,不传,则报错如下age不传值就报错了,但是当你把require设置为false的时候就访问正常了:
3:@RequestParam 不指定 name,获取所有参数值,这个开发中不建议大家这样使用,因为你不知道有几个具体参数值,但是某些功能的开发会使用到,要了解一下。
当我们想用一个 Map 来接收所有参数的之后,代码如下,@RequestParam 不用指定 name 的值,参数类型为 Map<String,String>,所有请求参数会以参数名称:值的方式丢在 Map 中。代码如下:
/**
* 不指定name,用于接收所有参数的值,
* 参数类型为Map<String,String>,key为请求中的参数名称,
* value为值
* @param paramMap
* @return
*/
@RequestMapping("/requestparam/test2")
public Map<String, String> test2(@RequestParam Map<String, String> paramMap) {
return paramMap;
}
我们使用postman调用一下如下图:
当你直接使用map接收的时候,Interests或者pets只会取我们传递的第一个值,这样在开发中就有问题了,我们明明要给后台传输多个值,但是没收到。这个时候你就想了这是严重的bug啊,开发中万一我们一定要使用map接收呢?别慌还有下面使用方法的。
3.2:@RequestParam 不指定 name,获取所有参数值。这个接口的参数是MultiValueMap类型。代码如下:
/**
* 不指定name,用于接收所有参数的值,
* 参数类型为MultiValueMap<String, String>:
* key为请求中的参数名称,value为值的集合List<String>
* @param paramMap
* @return
*/
@RequestMapping(value = "/requestparam/test3", produces = MediaType.APPLICATION_JSON_VALUE)
public MultiValueMap<String, String> test3(@RequestParam MultiValueMap<String, String> paramMap) {
return paramMap;
}
使用postman调用一下如下图:
参数是MultiValueMap类型,这玩意是干啥的?好像很陌生啊,哈哈再陌生的东西,把其源码放出来,瞬间明了了,如下代码,可以看出来MultiValueMap相当于Map<String,List<String>>。大家可以记住这种数据结构,开发中遇到这种数据结构的功能,直接使用MultiValueMap十分的好用。
public interface MultiValueMap<K, V> extends Map<K, List<V>> {
/**
* Return the first value for the given key.
* @param key the key
* @return the first value for the specified key, or {@code null} if none
*/
@Nullable
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的用法了吧。