Skip to content

Commit ab92754

Browse files
committed
@MatrixVariable resolvers for WebFlux
The information was already parsed and available in a request attribute but until now there were no argument resolvers to expose it. Issue: SPR-16005
1 parent c7a1526 commit ab92754

File tree

14 files changed

+865
-136
lines changed

14 files changed

+865
-136
lines changed

spring-web/src/test/java/org/springframework/web/method/MvcAnnotationPredicates.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.core.MethodParameter;
2323
import org.springframework.core.annotation.AnnotatedElementUtils;
2424
import org.springframework.http.HttpStatus;
25+
import org.springframework.web.bind.annotation.MatrixVariable;
2526
import org.springframework.web.bind.annotation.ModelAttribute;
2627
import org.springframework.web.bind.annotation.RequestBody;
2728
import org.springframework.web.bind.annotation.RequestMapping;
@@ -61,6 +62,9 @@ public static RequestPartPredicate requestPart() {
6162
return new RequestPartPredicate();
6263
}
6364

65+
public static MatrixVariablePredicate matrixAttribute() {
66+
return new MatrixVariablePredicate();
67+
}
6468

6569
// Method predicates
6670

@@ -305,4 +309,40 @@ public boolean test(Method method) {
305309
}
306310
}
307311

312+
public static class MatrixVariablePredicate implements Predicate<MethodParameter> {
313+
314+
private String name;
315+
316+
private String pathVar;
317+
318+
319+
public MatrixVariablePredicate name(String name) {
320+
this.name = name;
321+
return this;
322+
}
323+
324+
public MatrixVariablePredicate noName() {
325+
this.name = "";
326+
return this;
327+
}
328+
329+
public MatrixVariablePredicate pathVar(String name) {
330+
this.pathVar = name;
331+
return this;
332+
}
333+
334+
public MatrixVariablePredicate noPathVar() {
335+
this.pathVar = ValueConstants.DEFAULT_NONE;
336+
return this;
337+
}
338+
339+
@Override
340+
public boolean test(MethodParameter parameter) {
341+
MatrixVariable annotation = parameter.getParameterAnnotation(MatrixVariable.class);
342+
return annotation != null &&
343+
(this.name == null || this.name.equalsIgnoreCase(annotation.name())) &&
344+
(this.pathVar == null || this.pathVar.equalsIgnoreCase(annotation.pathVar()));
345+
}
346+
}
347+
308348
}

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractNamedValueSyncArgumentResolver.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,16 @@
3838
public abstract class AbstractNamedValueSyncArgumentResolver extends AbstractNamedValueArgumentResolver
3939
implements SyncHandlerMethodArgumentResolver {
4040

41+
4142
/**
4243
* @param factory a bean factory to use for resolving ${...}
4344
* placeholder and #{...} SpEL expressions in default values;
4445
* or {@code null} if default values are not expected to have expressions
4546
* @param registry for checking reactive type wrappers
4647
*/
47-
protected AbstractNamedValueSyncArgumentResolver(@Nullable ConfigurableBeanFactory factory, ReactiveAdapterRegistry registry) {
48+
protected AbstractNamedValueSyncArgumentResolver(@Nullable ConfigurableBeanFactory factory,
49+
ReactiveAdapterRegistry registry) {
50+
4851
super(factory, registry);
4952
}
5053

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ControllerMethodResolver.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ private void addResolversTo(ArgumentResolverRegistrar registrar,
144144
registrar.add(new RequestParamMapMethodArgumentResolver(reactiveRegistry));
145145
registrar.add(new PathVariableMethodArgumentResolver(beanFactory, reactiveRegistry));
146146
registrar.add(new PathVariableMapMethodArgumentResolver(reactiveRegistry));
147+
registrar.add(new MatrixVariableMethodArgumentResolver(beanFactory, reactiveRegistry));
148+
registrar.add(new MatrixVariableMapMethodArgumentResolver(reactiveRegistry));
147149
registrar.addIfRequestBody(readers -> new RequestBodyArgumentResolver(readers, reactiveRegistry));
148150
registrar.addIfRequestBody(readers -> new RequestPartMethodArgumentResolver(readers, reactiveRegistry));
149151
registrar.addIfModelAttribute(() -> new ModelAttributeMethodArgumentResolver(reactiveRegistry, false));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright 2002-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.web.reactive.result.method.annotation;
17+
18+
import java.util.Collections;
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
import org.springframework.core.MethodParameter;
23+
import org.springframework.core.ReactiveAdapterRegistry;
24+
import org.springframework.core.ResolvableType;
25+
import org.springframework.lang.Nullable;
26+
import org.springframework.util.Assert;
27+
import org.springframework.util.CollectionUtils;
28+
import org.springframework.util.LinkedMultiValueMap;
29+
import org.springframework.util.MultiValueMap;
30+
import org.springframework.util.StringUtils;
31+
import org.springframework.web.bind.annotation.MatrixVariable;
32+
import org.springframework.web.bind.annotation.ValueConstants;
33+
import org.springframework.web.reactive.BindingContext;
34+
import org.springframework.web.reactive.HandlerMapping;
35+
import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolverSupport;
36+
import org.springframework.web.reactive.result.method.SyncHandlerMethodArgumentResolver;
37+
import org.springframework.web.server.ServerWebExchange;
38+
39+
/**
40+
* Resolves arguments of type {@link Map} annotated with {@link MatrixVariable
41+
* @MatrixVariable} where the annotation does not specify a name. In other words
42+
* the purpose of this resolver is to provide access to multiple matrix
43+
* variables, either all or associted with a specific path variable.
44+
*
45+
* <p>When a name is specified, an argument of type Map is considered to be an
46+
* single attribute with a Map value, and is resolved by
47+
* {@link MatrixVariableMethodArgumentResolver} instead.
48+
*
49+
* @author Rossen Stoyanchev
50+
* @since 5.0.1
51+
* @see MatrixVariableMethodArgumentResolver
52+
*/
53+
public class MatrixVariableMapMethodArgumentResolver extends HandlerMethodArgumentResolverSupport
54+
implements SyncHandlerMethodArgumentResolver {
55+
56+
57+
public MatrixVariableMapMethodArgumentResolver(ReactiveAdapterRegistry registry) {
58+
super(registry);
59+
}
60+
61+
62+
@Override
63+
public boolean supportsParameter(MethodParameter parameter) {
64+
return checkAnnotatedParamNoReactiveWrapper(parameter, MatrixVariable.class,
65+
(annot, type) -> (Map.class.isAssignableFrom(type) && !StringUtils.hasText(annot.name())));
66+
}
67+
68+
@Nullable
69+
@Override
70+
public Object resolveArgumentValue(MethodParameter parameter, BindingContext bindingContext,
71+
ServerWebExchange exchange) {
72+
73+
Map<String, MultiValueMap<String, String>> matrixVariables =
74+
exchange.getAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE);
75+
76+
if (CollectionUtils.isEmpty(matrixVariables)) {
77+
return Collections.emptyMap();
78+
}
79+
80+
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
81+
MatrixVariable annotation = parameter.getParameterAnnotation(MatrixVariable.class);
82+
Assert.state(annotation != null, "No MatrixVariable annotation");
83+
String pathVariable = annotation.pathVar();
84+
85+
if (!pathVariable.equals(ValueConstants.DEFAULT_NONE)) {
86+
MultiValueMap<String, String> mapForPathVariable = matrixVariables.get(pathVariable);
87+
if (mapForPathVariable == null) {
88+
return Collections.emptyMap();
89+
}
90+
map.putAll(mapForPathVariable);
91+
}
92+
else {
93+
for (MultiValueMap<String, String> vars : matrixVariables.values()) {
94+
for (String name : vars.keySet()) {
95+
for (String value : vars.get(name)) {
96+
map.add(name, value);
97+
}
98+
}
99+
}
100+
}
101+
102+
return (isSingleValueMap(parameter) ? map.toSingleValueMap() : map);
103+
}
104+
105+
private boolean isSingleValueMap(MethodParameter parameter) {
106+
if (!MultiValueMap.class.isAssignableFrom(parameter.getParameterType())) {
107+
ResolvableType[] genericTypes = ResolvableType.forMethodParameter(parameter).getGenerics();
108+
if (genericTypes.length == 2) {
109+
Class<?> declaredClass = genericTypes[1].getRawClass();
110+
return (declaredClass == null || !List.class.isAssignableFrom(declaredClass));
111+
}
112+
}
113+
return false;
114+
}
115+
116+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright 2002-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.web.reactive.result.method.annotation;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
23+
import org.springframework.core.MethodParameter;
24+
import org.springframework.core.ReactiveAdapterRegistry;
25+
import org.springframework.lang.Nullable;
26+
import org.springframework.util.Assert;
27+
import org.springframework.util.CollectionUtils;
28+
import org.springframework.util.MultiValueMap;
29+
import org.springframework.util.StringUtils;
30+
import org.springframework.web.bind.annotation.MatrixVariable;
31+
import org.springframework.web.bind.annotation.ValueConstants;
32+
import org.springframework.web.reactive.HandlerMapping;
33+
import org.springframework.web.server.ServerErrorException;
34+
import org.springframework.web.server.ServerWebExchange;
35+
import org.springframework.web.server.ServerWebInputException;
36+
37+
/**
38+
* Resolves arguments annotated with {@link MatrixVariable @MatrixVariable}.
39+
*
40+
* <p>If the method parameter is of type {@link Map} it will by resolved by
41+
* {@link MatrixVariableMapMethodArgumentResolver} instead unless the annotation
42+
* specifies a name in which case it is considered to be a single attribute of
43+
* type map (vs multiple attributes collected in a map).
44+
*
45+
* @author Rossen Stoyanchev
46+
* @since 5.0.1
47+
* @see MatrixVariableMapMethodArgumentResolver
48+
*/
49+
public class MatrixVariableMethodArgumentResolver extends AbstractNamedValueSyncArgumentResolver {
50+
51+
52+
protected MatrixVariableMethodArgumentResolver(@Nullable ConfigurableBeanFactory factory,
53+
ReactiveAdapterRegistry registry) {
54+
55+
super(factory, registry);
56+
}
57+
58+
59+
@Override
60+
public boolean supportsParameter(MethodParameter parameter) {
61+
return checkAnnotatedParamNoReactiveWrapper(parameter, MatrixVariable.class,
62+
(annot, type) -> !Map.class.isAssignableFrom(type) || StringUtils.hasText(annot.name()));
63+
}
64+
65+
66+
@Override
67+
protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
68+
MatrixVariable ann = parameter.getParameterAnnotation(MatrixVariable.class);
69+
Assert.state(ann != null, "No MatrixVariable annotation");
70+
return new MatrixVariableNamedValueInfo(ann);
71+
}
72+
73+
@Nullable
74+
@Override
75+
protected Object resolveNamedValue(String name, MethodParameter param, ServerWebExchange exchange) {
76+
77+
Map<String, MultiValueMap<String, String>> pathParameters =
78+
exchange.getAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE);
79+
80+
if (CollectionUtils.isEmpty(pathParameters)) {
81+
return null;
82+
}
83+
84+
MatrixVariable ann = param.getParameterAnnotation(MatrixVariable.class);
85+
Assert.state(ann != null, "No MatrixVariable annotation");
86+
String pathVar = ann.pathVar();
87+
List<String> paramValues = null;
88+
89+
if (!pathVar.equals(ValueConstants.DEFAULT_NONE)) {
90+
if (pathParameters.containsKey(pathVar)) {
91+
paramValues = pathParameters.get(pathVar).get(name);
92+
}
93+
}
94+
else {
95+
boolean found = false;
96+
paramValues = new ArrayList<>();
97+
for (MultiValueMap<String, String> params : pathParameters.values()) {
98+
if (params.containsKey(name)) {
99+
if (found) {
100+
String paramType = param.getNestedParameterType().getName();
101+
throw new ServerErrorException(
102+
"Found more than one match for URI path parameter '" + name +
103+
"' for parameter type [" + paramType + "]. Use 'pathVar' attribute to disambiguate.");
104+
}
105+
paramValues.addAll(params.get(name));
106+
found = true;
107+
}
108+
}
109+
}
110+
111+
if (CollectionUtils.isEmpty(paramValues)) {
112+
return null;
113+
}
114+
else if (paramValues.size() == 1) {
115+
return paramValues.get(0);
116+
}
117+
else {
118+
return paramValues;
119+
}
120+
}
121+
122+
@Override
123+
protected void handleMissingValue(String name, MethodParameter parameter) throws ServerWebInputException {
124+
throw new ServerWebInputException("Missing matrix variable '" + name +
125+
"' for method parameter of type " + parameter.getNestedParameterType().getSimpleName());
126+
}
127+
128+
129+
private static class MatrixVariableNamedValueInfo extends NamedValueInfo {
130+
131+
private MatrixVariableNamedValueInfo(MatrixVariable annotation) {
132+
super(annotation.name(), annotation.required(), annotation.defaultValue());
133+
}
134+
}
135+
136+
}

0 commit comments

Comments
 (0)