Introduce alias for 'value' attribute in @MatrixVariable
Issue: SPR-11393
This commit is contained in:
parent
60eb9e9ca2
commit
1a56b47502
|
@ -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,6 +22,8 @@ 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
|
||||
* 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.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Sam Brannen
|
||||
* @since 3.2
|
||||
*/
|
||||
@Target(ElementType.PARAMETER)
|
||||
|
@ -45,10 +48,19 @@ import java.lang.annotation.Target;
|
|||
public @interface MatrixVariable {
|
||||
|
||||
/**
|
||||
* The name of the matrix variable.
|
||||
* Alias for {@link #name}.
|
||||
*/
|
||||
@AliasFor(attribute = "name")
|
||||
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,
|
||||
* 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.
|
||||
* <p>Default is {@code true}, leading to an exception thrown in case
|
||||
* of the variable missing in the request. Switch this to {@code false}
|
||||
* if you prefer a {@code null} in case of the variable missing.
|
||||
* <p>Alternatively, provide a {@link #defaultValue() defaultValue},
|
||||
* which implicitly sets this flag to {@code false}.
|
||||
* <p>Default is {@code true}, leading to an exception being thrown in
|
||||
* case the variable is missing in the request. Switch this to {@code false}
|
||||
* if you prefer a {@code null} if the variable is missing.
|
||||
* <p>Alternatively, provide a {@link #defaultValue}, which implicitly sets
|
||||
* this flag to {@code false}.
|
||||
*/
|
||||
boolean required() default true;
|
||||
|
||||
/**
|
||||
* The default value to use as a fallback. Supplying a default value implicitly
|
||||
* sets {@link #required()} to false.
|
||||
* The default value to use as a fallback.
|
||||
* <p>Supplying a default value implicitly sets {@link #required} to
|
||||
* {@code false}.
|
||||
*/
|
||||
String defaultValue() default ValueConstants.DEFAULT_NONE;
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
@ -35,8 +35,8 @@ import org.springframework.web.servlet.HandlerMapping;
|
|||
|
||||
/**
|
||||
* Resolves method arguments of type Map annotated with
|
||||
* {@link MatrixVariable @MatrixVariable} where the annotation the does not
|
||||
* specify a name. If a name specified then the argument will by resolved by the
|
||||
* {@link MatrixVariable @MatrixVariable} where the annotation does not
|
||||
* specify a name. If a name is specified then the argument will by resolved by the
|
||||
* {@link MatrixVariableMethodArgumentResolver} instead.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
|
@ -46,10 +46,10 @@ public class MatrixVariableMapMethodArgumentResolver implements HandlerMethodArg
|
|||
|
||||
@Override
|
||||
public boolean supportsParameter(MethodParameter parameter) {
|
||||
MatrixVariable paramAnnot = parameter.getParameterAnnotation(MatrixVariable.class);
|
||||
if (paramAnnot != null) {
|
||||
MatrixVariable matrixVariable = parameter.getParameterAnnotation(MatrixVariable.class);
|
||||
if (matrixVariable != null) {
|
||||
if (Map.class.isAssignableFrom(parameter.getParameterType())) {
|
||||
return !StringUtils.hasText(paramAnnot.value());
|
||||
return !StringUtils.hasText(matrixVariable.name());
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
|
|
@ -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.
|
||||
|
@ -33,12 +33,13 @@ import org.springframework.web.method.annotation.AbstractNamedValueMethodArgumen
|
|||
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
|
||||
* by resolved by the {@link MatrixVariableMapMethodArgumentResolver} instead.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Sam Brannen
|
||||
* @since 3.2
|
||||
*/
|
||||
public class MatrixVariableMethodArgumentResolver extends AbstractNamedValueMethodArgumentResolver {
|
||||
|
@ -53,8 +54,8 @@ public class MatrixVariableMethodArgumentResolver extends AbstractNamedValueMeth
|
|||
return false;
|
||||
}
|
||||
if (Map.class.isAssignableFrom(parameter.getParameterType())) {
|
||||
String paramName = parameter.getParameterAnnotation(MatrixVariable.class).value();
|
||||
return StringUtils.hasText(paramName);
|
||||
String variableName = parameter.getParameterAnnotation(MatrixVariable.class).name();
|
||||
return StringUtils.hasText(variableName);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -62,7 +63,7 @@ public class MatrixVariableMethodArgumentResolver extends AbstractNamedValueMeth
|
|||
@Override
|
||||
protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
|
||||
MatrixVariable annotation = parameter.getParameterAnnotation(MatrixVariable.class);
|
||||
return new PathParamNamedValueInfo(annotation);
|
||||
return new MatrixVariableNamedValueInfo(annotation);
|
||||
}
|
||||
|
||||
@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) {
|
||||
super(annotation.value(), annotation.required(), annotation.defaultValue());
|
||||
private MatrixVariableNamedValueInfo(MatrixVariable annotation) {
|
||||
super(annotation.name(), annotation.required(), annotation.defaultValue());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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.
|
||||
|
@ -152,7 +152,7 @@ public class MatrixVariablesMethodArgumentResolverTests {
|
|||
public void handle(
|
||||
String stringArg,
|
||||
@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) {
|
||||
}
|
||||
|
||||
}
|
|
@ -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.
|
||||
|
@ -397,8 +397,8 @@ public class UriTemplateServletAnnotationControllerHandlerMethodTests extends Ab
|
|||
public void handle(@PathVariable("hotel") String hotel,
|
||||
@PathVariable int booking,
|
||||
@PathVariable String other,
|
||||
@MatrixVariable(value="q", pathVar="hotel") int qHotel,
|
||||
@MatrixVariable(value="q", pathVar="other") int qOther,
|
||||
@MatrixVariable(name = "q", pathVar = "hotel") int qHotel,
|
||||
@MatrixVariable(name = "q", pathVar = "other") int qOther,
|
||||
Writer writer) throws IOException {
|
||||
assertEquals("Invalid path variable value", "42", hotel);
|
||||
assertEquals("Invalid path variable value", 21, booking);
|
||||
|
|
Loading…
Reference in New Issue