From 25a5d9d759a93bf0219c5283765c02899d7fae5b Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 31 May 2015 16:07:09 +0200 Subject: [PATCH] Introduce alias for 'value' attribute in @RequestHeader Issue: SPR-11393 --- .../web/bind/annotation/RequestHeader.java | 18 +++++++++++++++--- .../support/HandlerMethodInvoker.java | 2 +- .../RequestHeaderMethodArgumentResolver.java | 4 ++-- ...questHeaderMethodArgumentResolverTests.java | 8 ++++---- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestHeader.java b/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestHeader.java index 7276bc34b7..a3e433216d 100644 --- a/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestHeader.java +++ b/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestHeader.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. @@ -22,9 +22,12 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.springframework.core.annotation.AliasFor; + /** * Annotation which indicates that a method parameter should be bound to a web request header. - * Supported for annotated handler methods in Servlet and Portlet environments. + * + *

Supported for annotated handler methods in Servlet and Portlet environments. * *

If the method parameter is {@link java.util.Map Map<String, String>} or * {@link org.springframework.util.MultiValueMap MultiValueMap<String, String>}, @@ -32,6 +35,7 @@ import java.lang.annotation.Target; * populated with all header names and values. * * @author Juergen Hoeller + * @author Sam Brannen * @since 3.0 * @see RequestMapping * @see RequestParam @@ -45,10 +49,18 @@ import java.lang.annotation.Target; public @interface RequestHeader { /** - * The name of the request header to bind to. + * Alias for {@link #name}. */ + @AliasFor(attribute = "name") String value() default ""; + /** + * The name of the request header to bind to. + * @since 4.2 + */ + @AliasFor(attribute = "value") + String name() default ""; + /** * Whether the header 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 5b3f22a957..219324ed47 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 @@ -267,7 +267,7 @@ public class HandlerMethodInvoker { } else if (RequestHeader.class.isInstance(paramAnn)) { RequestHeader requestHeader = (RequestHeader) paramAnn; - headerName = requestHeader.value(); + headerName = requestHeader.name(); required = requestHeader.required(); defaultValue = parseDefaultValueAttribute(requestHeader.defaultValue()); annotationsFound++; diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolver.java b/spring-web/src/main/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolver.java index 5ac1cc535f..c209c52c06 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolver.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolver.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. @@ -86,7 +86,7 @@ public class RequestHeaderMethodArgumentResolver extends AbstractNamedValueMetho private static class RequestHeaderNamedValueInfo extends NamedValueInfo { private RequestHeaderNamedValueInfo(RequestHeader 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/RequestHeaderMethodArgumentResolverTests.java b/spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolverTests.java index 0806e60e45..0895749867 100644 --- a/spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolverTests.java +++ b/spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolverTests.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. @@ -141,10 +141,10 @@ public class RequestHeaderMethodArgumentResolverTests { resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null); } - public void params(@RequestHeader(value = "name", defaultValue = "bar") String param1, + public void params(@RequestHeader(name = "name", defaultValue = "bar") String param1, @RequestHeader("name") String[] param2, - @RequestHeader(value = "name", defaultValue="#{systemProperties.systemProperty}") String param3, - @RequestHeader(value = "name", defaultValue="#{request.contextPath}") String param4, + @RequestHeader(name = "name", defaultValue="#{systemProperties.systemProperty}") String param3, + @RequestHeader(name = "name", defaultValue="#{request.contextPath}") String param4, @RequestHeader("name") Map unsupported) { } -- GitLab