Introduce alias for 'value' attribute in @MatrixVariable

Issue: SPR-11393
This commit is contained in:
Sam Brannen 2015-05-31 18:50:35 +02:00
parent 60eb9e9ca2
commit 1a56b47502
5 changed files with 42 additions and 28 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,6 +22,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
/** /**
* Annotation which indicates that a method parameter should be bound to a * Annotation which indicates that a method parameter should be bound to a
* name-value pair within a path segment. Supported for {@link RequestMapping} * name-value pair within a path segment. Supported for {@link RequestMapping}
@ -37,6 +39,7 @@ import java.lang.annotation.Target;
* matrix variable names and values. * matrix variable names and values.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Sam Brannen
* @since 3.2 * @since 3.2
*/ */
@Target(ElementType.PARAMETER) @Target(ElementType.PARAMETER)
@ -45,10 +48,19 @@ import java.lang.annotation.Target;
public @interface MatrixVariable { public @interface MatrixVariable {
/** /**
* The name of the matrix variable. * Alias for {@link #name}.
*/ */
@AliasFor(attribute = "name")
String value() default ""; String value() default "";
/**
* The name of the matrix variable.
* @since 4.2
* @see #value
*/
@AliasFor(attribute = "value")
String name() default "";
/** /**
* The name of the URI path variable where the matrix variable is located, * The name of the URI path variable where the matrix variable is located,
* if necessary for disambiguation (e.g. a matrix variable with the same * if necessary for disambiguation (e.g. a matrix variable with the same
@ -58,17 +70,18 @@ public @interface MatrixVariable {
/** /**
* Whether the matrix variable is required. * Whether the matrix variable is required.
* <p>Default is {@code true}, leading to an exception thrown in case * <p>Default is {@code true}, leading to an exception being thrown in
* of the variable missing in the request. Switch this to {@code false} * case the variable is missing in the request. Switch this to {@code false}
* if you prefer a {@code null} in case of the variable missing. * if you prefer a {@code null} if the variable is missing.
* <p>Alternatively, provide a {@link #defaultValue() defaultValue}, * <p>Alternatively, provide a {@link #defaultValue}, which implicitly sets
* which implicitly sets this flag to {@code false}. * this flag to {@code false}.
*/ */
boolean required() default true; boolean required() default true;
/** /**
* The default value to use as a fallback. Supplying a default value implicitly * The default value to use as a fallback.
* sets {@link #required()} to false. * <p>Supplying a default value implicitly sets {@link #required} to
* {@code false}.
*/ */
String defaultValue() default ValueConstants.DEFAULT_NONE; String defaultValue() default ValueConstants.DEFAULT_NONE;

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -35,8 +35,8 @@ import org.springframework.web.servlet.HandlerMapping;
/** /**
* Resolves method arguments of type Map annotated with * Resolves method arguments of type Map annotated with
* {@link MatrixVariable @MatrixVariable} where the annotation the does not * {@link MatrixVariable @MatrixVariable} where the annotation does not
* specify a name. If a name specified then the argument will by resolved by the * specify a name. If a name is specified then the argument will by resolved by the
* {@link MatrixVariableMethodArgumentResolver} instead. * {@link MatrixVariableMethodArgumentResolver} instead.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
@ -46,10 +46,10 @@ public class MatrixVariableMapMethodArgumentResolver implements HandlerMethodArg
@Override @Override
public boolean supportsParameter(MethodParameter parameter) { public boolean supportsParameter(MethodParameter parameter) {
MatrixVariable paramAnnot = parameter.getParameterAnnotation(MatrixVariable.class); MatrixVariable matrixVariable = parameter.getParameterAnnotation(MatrixVariable.class);
if (paramAnnot != null) { if (matrixVariable != null) {
if (Map.class.isAssignableFrom(parameter.getParameterType())) { if (Map.class.isAssignableFrom(parameter.getParameterType())) {
return !StringUtils.hasText(paramAnnot.value()); return !StringUtils.hasText(matrixVariable.name());
} }
} }
return false; return false;

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -33,12 +33,13 @@ import org.springframework.web.method.annotation.AbstractNamedValueMethodArgumen
import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.HandlerMapping;
/** /**
* Resolves method arguments annotated with an {@link MatrixVariable @PathParam}. * Resolves method arguments annotated with {@link MatrixVariable @MatrixVariable}.
* *
* <p>If the method parameter is of type Map and no name is specified, then it will * <p>If the method parameter is of type Map and no name is specified, then it will
* by resolved by the {@link MatrixVariableMapMethodArgumentResolver} instead. * by resolved by the {@link MatrixVariableMapMethodArgumentResolver} instead.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Sam Brannen
* @since 3.2 * @since 3.2
*/ */
public class MatrixVariableMethodArgumentResolver extends AbstractNamedValueMethodArgumentResolver { public class MatrixVariableMethodArgumentResolver extends AbstractNamedValueMethodArgumentResolver {
@ -53,8 +54,8 @@ public class MatrixVariableMethodArgumentResolver extends AbstractNamedValueMeth
return false; return false;
} }
if (Map.class.isAssignableFrom(parameter.getParameterType())) { if (Map.class.isAssignableFrom(parameter.getParameterType())) {
String paramName = parameter.getParameterAnnotation(MatrixVariable.class).value(); String variableName = parameter.getParameterAnnotation(MatrixVariable.class).name();
return StringUtils.hasText(paramName); return StringUtils.hasText(variableName);
} }
return true; return true;
} }
@ -62,7 +63,7 @@ public class MatrixVariableMethodArgumentResolver extends AbstractNamedValueMeth
@Override @Override
protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) { protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
MatrixVariable annotation = parameter.getParameterAnnotation(MatrixVariable.class); MatrixVariable annotation = parameter.getParameterAnnotation(MatrixVariable.class);
return new PathParamNamedValueInfo(annotation); return new MatrixVariableNamedValueInfo(annotation);
} }
@Override @Override
@ -120,10 +121,10 @@ public class MatrixVariableMethodArgumentResolver extends AbstractNamedValueMeth
} }
private static class PathParamNamedValueInfo extends NamedValueInfo { private static class MatrixVariableNamedValueInfo extends NamedValueInfo {
private PathParamNamedValueInfo(MatrixVariable annotation) { private MatrixVariableNamedValueInfo(MatrixVariable annotation) {
super(annotation.value(), annotation.required(), annotation.defaultValue()); super(annotation.name(), annotation.required(), annotation.defaultValue());
} }
} }
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -152,7 +152,7 @@ public class MatrixVariablesMethodArgumentResolverTests {
public void handle( public void handle(
String stringArg, String stringArg,
@MatrixVariable List<String> colors, @MatrixVariable List<String> colors,
@MatrixVariable(value="year", pathVar="cars", required=false, defaultValue="2013") int preferredYear) { @MatrixVariable(name = "year", pathVar = "cars", required = false, defaultValue = "2013") int preferredYear) {
} }
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -397,8 +397,8 @@ public class UriTemplateServletAnnotationControllerHandlerMethodTests extends Ab
public void handle(@PathVariable("hotel") String hotel, public void handle(@PathVariable("hotel") String hotel,
@PathVariable int booking, @PathVariable int booking,
@PathVariable String other, @PathVariable String other,
@MatrixVariable(value="q", pathVar="hotel") int qHotel, @MatrixVariable(name = "q", pathVar = "hotel") int qHotel,
@MatrixVariable(value="q", pathVar="other") int qOther, @MatrixVariable(name = "q", pathVar = "other") int qOther,
Writer writer) throws IOException { Writer writer) throws IOException {
assertEquals("Invalid path variable value", "42", hotel); assertEquals("Invalid path variable value", "42", hotel);
assertEquals("Invalid path variable value", 21, booking); assertEquals("Invalid path variable value", 21, booking);