diff --git a/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestParam.java b/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestParam.java index b1395b49852..170017e8196 100644 --- a/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestParam.java +++ b/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestParam.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,10 +23,13 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.util.Map; +import org.springframework.core.annotation.AliasFor; + /** * Annotation which indicates that a method parameter should be bound to a web - * request parameter. Supported for annotated handler methods in Servlet and - * Portlet environments. + * request parameter. + * + *
Supported for annotated handler methods in Servlet and Portlet environments. * *
If the method parameter type is {@link Map} and a request parameter name * is specified, then the request parameter value is converted to a {@link Map} @@ -39,6 +42,7 @@ import java.util.Map; * * @author Arjen Poutsma * @author Juergen Hoeller + * @author Sam Brannen * @since 2.5 * @see RequestMapping * @see RequestHeader @@ -53,10 +57,18 @@ import java.util.Map; public @interface RequestParam { /** - * The name of the request parameter to bind to. + * Alias for {@link #name}. */ + @AliasFor(attribute = "name") String value() default ""; + /** + * The name of the request parameter to bind to. + * @since 4.2 + */ + @AliasFor(attribute = "value") + String name() default ""; + /** * Whether the parameter is required. *
Default is {@code true}, leading to an exception thrown in case
diff --git a/spring-web/src/main/java/org/springframework/web/bind/annotation/support/HandlerMethodInvoker.java b/spring-web/src/main/java/org/springframework/web/bind/annotation/support/HandlerMethodInvoker.java
index 219324ed47d..ff66d18c9ba 100644
--- a/spring-web/src/main/java/org/springframework/web/bind/annotation/support/HandlerMethodInvoker.java
+++ b/spring-web/src/main/java/org/springframework/web/bind/annotation/support/HandlerMethodInvoker.java
@@ -260,7 +260,7 @@ public class HandlerMethodInvoker {
for (Annotation paramAnn : paramAnns) {
if (RequestParam.class.isInstance(paramAnn)) {
RequestParam requestParam = (RequestParam) paramAnn;
- paramName = requestParam.value();
+ paramName = requestParam.name();
required = requestParam.required();
defaultValue = parseDefaultValueAttribute(requestParam.defaultValue());
annotationsFound++;
@@ -432,7 +432,7 @@ public class HandlerMethodInvoker {
for (Annotation paramAnn : paramAnns) {
if (RequestParam.class.isInstance(paramAnn)) {
RequestParam requestParam = (RequestParam) paramAnn;
- paramName = requestParam.value();
+ paramName = requestParam.name();
paramRequired = requestParam.required();
paramDefaultValue = parseDefaultValueAttribute(requestParam.defaultValue());
break;
diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMapMethodArgumentResolver.java b/spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMapMethodArgumentResolver.java
index 3038be27015..39fce258011 100644
--- a/spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMapMethodArgumentResolver.java
+++ b/spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMapMethodArgumentResolver.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2014 the original author or authors.
+ * Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -49,10 +49,10 @@ public class RequestParamMapMethodArgumentResolver implements HandlerMethodArgum
@Override
public boolean supportsParameter(MethodParameter parameter) {
- RequestParam ann = parameter.getParameterAnnotation(RequestParam.class);
- if (ann != null) {
+ RequestParam requestParam = parameter.getParameterAnnotation(RequestParam.class);
+ if (requestParam != null) {
if (Map.class.isAssignableFrom(parameter.getParameterType())) {
- return !StringUtils.hasText(ann.value());
+ return !StringUtils.hasText(requestParam.name());
}
}
return false;
diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java b/spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java
index f861a776fde..e2671f3b081 100644
--- a/spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java
+++ b/spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java
@@ -127,7 +127,7 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod
Class> paramType = parameter.getParameterType();
if (parameter.hasParameterAnnotation(RequestParam.class)) {
if (Map.class.isAssignableFrom(paramType)) {
- String paramName = parameter.getParameterAnnotation(RequestParam.class).value();
+ String paramName = parameter.getParameterAnnotation(RequestParam.class).name();
return StringUtils.hasText(paramName);
}
else {
@@ -261,8 +261,8 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod
return;
}
- RequestParam ann = parameter.getParameterAnnotation(RequestParam.class);
- String name = (ann == null || StringUtils.isEmpty(ann.value()) ? parameter.getParameterName() : ann.value());
+ RequestParam requestParam = parameter.getParameterAnnotation(RequestParam.class);
+ String name = (requestParam == null || StringUtils.isEmpty(requestParam.name()) ? parameter.getParameterName() : requestParam.name());
if (value == null) {
builder.queryParam(name);
@@ -301,7 +301,7 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod
}
public RequestParamNamedValueInfo(RequestParam annotation) {
- super(annotation.value(), annotation.required(), annotation.defaultValue());
+ super(annotation.name(), annotation.required(), annotation.defaultValue());
}
}
diff --git a/spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java b/spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java
index 23e6fbccb9d..926ede0ebb2 100644
--- a/spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java
+++ b/spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2014 the original author or authors.
+ * Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
+
import javax.servlet.http.Part;
import org.junit.Before;
@@ -427,6 +428,7 @@ public class RequestParamMethodArgumentResolverTests {
}
@Test
+ @SuppressWarnings("rawtypes")
public void resolveOptional() throws Exception {
ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
initializer.setConversionService(new DefaultConversionService());
@@ -443,24 +445,24 @@ public class RequestParamMethodArgumentResolverTests {
}
- public void params(@RequestParam(value = "name", defaultValue = "bar") String param1,
+ public void params(@RequestParam(name = "name", defaultValue = "bar") String param1,
@RequestParam("name") String[] param2,
@RequestParam("name") Map, ?> param3,
- @RequestParam(value = "mfile") MultipartFile param4,
- @RequestParam(value = "mfilelist") List