Browse Source

Introduce alias for 'value' attribute in @RequestParam

Issue: SPR-11393
pull/811/head
Sam Brannen 11 years ago
parent
commit
034e0e2cf4
  1. 20
      spring-web/src/main/java/org/springframework/web/bind/annotation/RequestParam.java
  2. 4
      spring-web/src/main/java/org/springframework/web/bind/annotation/support/HandlerMethodInvoker.java
  3. 8
      spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMapMethodArgumentResolver.java
  4. 8
      spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java
  5. 24
      spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java

20
spring-web/src/main/java/org/springframework/web/bind/annotation/RequestParam.java

@ -1,5 +1,5 @@ @@ -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; @@ -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.
*
* <p>Supported for annotated handler methods in Servlet and Portlet environments.
*
* <p>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; @@ -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; @@ -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.
* <p>Default is {@code true}, leading to an exception thrown in case

4
spring-web/src/main/java/org/springframework/web/bind/annotation/support/HandlerMethodInvoker.java

@ -260,7 +260,7 @@ public class HandlerMethodInvoker { @@ -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 { @@ -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;

8
spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMapMethodArgumentResolver.java

@ -1,5 +1,5 @@ @@ -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 @@ -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;

8
spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java

@ -127,7 +127,7 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod @@ -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 @@ -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 @@ -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());
}
}

24
spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java

@ -1,5 +1,5 @@ @@ -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; @@ -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 { @@ -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 { @@ -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<MultipartFile> param5,
@RequestParam(value = "mfilearray") MultipartFile[] param6,
@RequestParam(value = "pfile") Part param7,
@RequestParam(value = "pfilelist") List<Part> param8,
@RequestParam(value = "pfilearray") Part[] param9,
@RequestParam("mfile") MultipartFile param4,
@RequestParam("mfilelist") List<MultipartFile> param5,
@RequestParam("mfilearray") MultipartFile[] param6,
@RequestParam("pfile") Part param7,
@RequestParam("pfilelist") List<Part> param8,
@RequestParam("pfilearray") Part[] param9,
@RequestParam Map<?, ?> param10,
String stringNotAnnot,
MultipartFile multipartFileNotAnnot,
List<MultipartFile> multipartFileList,
Part part,
@RequestPart MultipartFile requestPartAnnot,
@RequestParam(value = "name") String paramRequired,
@RequestParam(value = "name", required=false) String paramNotRequired,
@RequestParam(value = "name") Optional<Integer> paramOptional) {
@RequestParam("name") String paramRequired,
@RequestParam(name = "name", required = false) String paramNotRequired,
@RequestParam("name") Optional<Integer> paramOptional) {
}
}

Loading…
Cancel
Save