org.springframework.web.bind.annotation.MatrixVariable#pathVar ( )源码实例Demo

下面列出了org.springframework.web.bind.annotation.MatrixVariable#pathVar ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@Nullable
@Override
public Object resolveArgumentValue(MethodParameter parameter, BindingContext bindingContext,
		ServerWebExchange exchange) {

	Map<String, MultiValueMap<String, String>> matrixVariables =
			exchange.getAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE);

	if (CollectionUtils.isEmpty(matrixVariables)) {
		return Collections.emptyMap();
	}

	MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
	MatrixVariable annotation = parameter.getParameterAnnotation(MatrixVariable.class);
	Assert.state(annotation != null, "No MatrixVariable annotation");
	String pathVariable = annotation.pathVar();

	if (!pathVariable.equals(ValueConstants.DEFAULT_NONE)) {
		MultiValueMap<String, String> mapForPathVariable = matrixVariables.get(pathVariable);
		if (mapForPathVariable == null) {
			return Collections.emptyMap();
		}
		map.putAll(mapForPathVariable);
	}
	else {
		for (MultiValueMap<String, String> vars : matrixVariables.values()) {
			vars.forEach((name, values) -> {
				for (String value : values) {
					map.add(name, value);
				}
			});
		}
	}

	return (isSingleValueMap(parameter) ? map.toSingleValueMap() : map);
}
 
@Override
@Nullable
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
		NativeWebRequest request, @Nullable WebDataBinderFactory binderFactory) throws Exception {

	@SuppressWarnings("unchecked")
	Map<String, MultiValueMap<String, String>> matrixVariables =
			(Map<String, MultiValueMap<String, String>>) request.getAttribute(
					HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);

	if (CollectionUtils.isEmpty(matrixVariables)) {
		return Collections.emptyMap();
	}

	MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
	MatrixVariable ann = parameter.getParameterAnnotation(MatrixVariable.class);
	Assert.state(ann != null, "No MatrixVariable annotation");
	String pathVariable = ann.pathVar();

	if (!pathVariable.equals(ValueConstants.DEFAULT_NONE)) {
		MultiValueMap<String, String> mapForPathVariable = matrixVariables.get(pathVariable);
		if (mapForPathVariable == null) {
			return Collections.emptyMap();
		}
		map.putAll(mapForPathVariable);
	}
	else {
		for (MultiValueMap<String, String> vars : matrixVariables.values()) {
			vars.forEach((name, values) -> {
				for (String value : values) {
					map.add(name, value);
				}
			});
		}
	}

	return (isSingleValueMap(parameter) ? map.toSingleValueMap() : map);
}
 
@Nullable
@Override
public Object resolveArgumentValue(MethodParameter parameter, BindingContext bindingContext,
		ServerWebExchange exchange) {

	Map<String, MultiValueMap<String, String>> matrixVariables =
			exchange.getAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE);

	if (CollectionUtils.isEmpty(matrixVariables)) {
		return Collections.emptyMap();
	}

	MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
	MatrixVariable annotation = parameter.getParameterAnnotation(MatrixVariable.class);
	Assert.state(annotation != null, "No MatrixVariable annotation");
	String pathVariable = annotation.pathVar();

	if (!pathVariable.equals(ValueConstants.DEFAULT_NONE)) {
		MultiValueMap<String, String> mapForPathVariable = matrixVariables.get(pathVariable);
		if (mapForPathVariable == null) {
			return Collections.emptyMap();
		}
		map.putAll(mapForPathVariable);
	}
	else {
		for (MultiValueMap<String, String> vars : matrixVariables.values()) {
			vars.forEach((name, values) -> {
				for (String value : values) {
					map.add(name, value);
				}
			});
		}
	}

	return (isSingleValueMap(parameter) ? map.toSingleValueMap() : map);
}
 
@Override
@Nullable
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
		NativeWebRequest request, @Nullable WebDataBinderFactory binderFactory) throws Exception {

	@SuppressWarnings("unchecked")
	Map<String, MultiValueMap<String, String>> matrixVariables =
			(Map<String, MultiValueMap<String, String>>) request.getAttribute(
					HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);

	if (CollectionUtils.isEmpty(matrixVariables)) {
		return Collections.emptyMap();
	}

	MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
	MatrixVariable ann = parameter.getParameterAnnotation(MatrixVariable.class);
	Assert.state(ann != null, "No MatrixVariable annotation");
	String pathVariable = ann.pathVar();

	if (!pathVariable.equals(ValueConstants.DEFAULT_NONE)) {
		MultiValueMap<String, String> mapForPathVariable = matrixVariables.get(pathVariable);
		if (mapForPathVariable == null) {
			return Collections.emptyMap();
		}
		map.putAll(mapForPathVariable);
	}
	else {
		for (MultiValueMap<String, String> vars : matrixVariables.values()) {
			vars.forEach((name, values) -> {
				for (String value : values) {
					map.add(name, value);
				}
			});
		}
	}

	return (isSingleValueMap(parameter) ? map.toSingleValueMap() : map);
}
 
@Nullable
@Override
protected Object resolveNamedValue(String name, MethodParameter param, ServerWebExchange exchange) {
	Map<String, MultiValueMap<String, String>> pathParameters =
			exchange.getAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE);
	if (CollectionUtils.isEmpty(pathParameters)) {
		return null;
	}

	MatrixVariable ann = param.getParameterAnnotation(MatrixVariable.class);
	Assert.state(ann != null, "No MatrixVariable annotation");
	String pathVar = ann.pathVar();
	List<String> paramValues = null;

	if (!pathVar.equals(ValueConstants.DEFAULT_NONE)) {
		if (pathParameters.containsKey(pathVar)) {
			paramValues = pathParameters.get(pathVar).get(name);
		}
	}
	else {
		boolean found = false;
		paramValues = new ArrayList<>();
		for (MultiValueMap<String, String> params : pathParameters.values()) {
			if (params.containsKey(name)) {
				if (found) {
					String paramType = param.getNestedParameterType().getName();
					throw new ServerErrorException(
							"Found more than one match for URI path parameter '" + name +
							"' for parameter type [" + paramType + "]. Use 'pathVar' attribute to disambiguate.",
							param, null);
				}
				paramValues.addAll(params.get(name));
				found = true;
			}
		}
	}

	if (CollectionUtils.isEmpty(paramValues)) {
		return null;
	}
	else if (paramValues.size() == 1) {
		return paramValues.get(0);
	}
	else {
		return paramValues;
	}
}
 
@Override
@SuppressWarnings("unchecked")
@Nullable
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {
	Map<String, MultiValueMap<String, String>> pathParameters = (Map<String, MultiValueMap<String, String>>)
			request.getAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
	if (CollectionUtils.isEmpty(pathParameters)) {
		return null;
	}

	MatrixVariable ann = parameter.getParameterAnnotation(MatrixVariable.class);
	Assert.state(ann != null, "No MatrixVariable annotation");
	String pathVar = ann.pathVar();
	List<String> paramValues = null;

	if (!pathVar.equals(ValueConstants.DEFAULT_NONE)) {
		if (pathParameters.containsKey(pathVar)) {
			paramValues = pathParameters.get(pathVar).get(name);
		}
	}
	else {
		boolean found = false;
		paramValues = new ArrayList<>();
		for (MultiValueMap<String, String> params : pathParameters.values()) {
			if (params.containsKey(name)) {
				if (found) {
					String paramType = parameter.getNestedParameterType().getName();
					throw new ServletRequestBindingException(
							"Found more than one match for URI path parameter '" + name +
							"' for parameter type [" + paramType + "]. Use 'pathVar' attribute to disambiguate.");
				}
				paramValues.addAll(params.get(name));
				found = true;
			}
		}
	}

	if (CollectionUtils.isEmpty(paramValues)) {
		return null;
	}
	else if (paramValues.size() == 1) {
		return paramValues.get(0);
	}
	else {
		return paramValues;
	}
}
 
@Nullable
@Override
protected Object resolveNamedValue(String name, MethodParameter param, ServerWebExchange exchange) {
	Map<String, MultiValueMap<String, String>> pathParameters =
			exchange.getAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE);
	if (CollectionUtils.isEmpty(pathParameters)) {
		return null;
	}

	MatrixVariable ann = param.getParameterAnnotation(MatrixVariable.class);
	Assert.state(ann != null, "No MatrixVariable annotation");
	String pathVar = ann.pathVar();
	List<String> paramValues = null;

	if (!pathVar.equals(ValueConstants.DEFAULT_NONE)) {
		if (pathParameters.containsKey(pathVar)) {
			paramValues = pathParameters.get(pathVar).get(name);
		}
	}
	else {
		boolean found = false;
		paramValues = new ArrayList<>();
		for (MultiValueMap<String, String> params : pathParameters.values()) {
			if (params.containsKey(name)) {
				if (found) {
					String paramType = param.getNestedParameterType().getName();
					throw new ServerErrorException(
							"Found more than one match for URI path parameter '" + name +
							"' for parameter type [" + paramType + "]. Use 'pathVar' attribute to disambiguate.",
							param, null);
				}
				paramValues.addAll(params.get(name));
				found = true;
			}
		}
	}

	if (CollectionUtils.isEmpty(paramValues)) {
		return null;
	}
	else if (paramValues.size() == 1) {
		return paramValues.get(0);
	}
	else {
		return paramValues;
	}
}
 
@Override
@SuppressWarnings("unchecked")
@Nullable
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {
	Map<String, MultiValueMap<String, String>> pathParameters = (Map<String, MultiValueMap<String, String>>)
			request.getAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
	if (CollectionUtils.isEmpty(pathParameters)) {
		return null;
	}

	MatrixVariable ann = parameter.getParameterAnnotation(MatrixVariable.class);
	Assert.state(ann != null, "No MatrixVariable annotation");
	String pathVar = ann.pathVar();
	List<String> paramValues = null;

	if (!pathVar.equals(ValueConstants.DEFAULT_NONE)) {
		if (pathParameters.containsKey(pathVar)) {
			paramValues = pathParameters.get(pathVar).get(name);
		}
	}
	else {
		boolean found = false;
		paramValues = new ArrayList<>();
		for (MultiValueMap<String, String> params : pathParameters.values()) {
			if (params.containsKey(name)) {
				if (found) {
					String paramType = parameter.getNestedParameterType().getName();
					throw new ServletRequestBindingException(
							"Found more than one match for URI path parameter '" + name +
							"' for parameter type [" + paramType + "]. Use 'pathVar' attribute to disambiguate.");
				}
				paramValues.addAll(params.get(name));
				found = true;
			}
		}
	}

	if (CollectionUtils.isEmpty(paramValues)) {
		return null;
	}
	else if (paramValues.size() == 1) {
		return paramValues.get(0);
	}
	else {
		return paramValues;
	}
}