下面列出了怎么用org.springframework.web.bind.annotation.MatrixVariable的API类实例代码及写法,或者点击链接到github查看源代码。
@RequestMapping(value = {"select/{selectType}", "select"}, method = RequestMethod.GET)
@PageableDefaults(sort = "weight=desc")
public String select(
Searchable searchable, Model model,
@PathVariable(value = "selectType") String selectType,
@MatrixVariable(value = "domId", pathVar = "selectType") String domId,
@MatrixVariable(value = "domName", pathVar = "selectType", required = false) String domName) {
this.permissionList.assertHasViewPermission();
model.addAttribute("selectType", selectType);
model.addAttribute("domId", domId);
model.addAttribute("domName", domName);
super.list(searchable, model);
return "showcase/product/category/select";
}
@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);
}
@GetMapping("/{one}/{two}/{three}-{four}")
public String matrixVar(
@MatrixVariable int p,
@MatrixVariable(name = "q", pathVar = "two") int q2,
@MatrixVariable(name = "q", pathVar = "four") int q4) {
return "p=" + p + ", q2=" + q2 + ", q4=" + q4;
}
@SuppressWarnings("unused")
void handle(
String stringArg,
@MatrixVariable Map<String, String> map,
@MatrixVariable MultiValueMap<String, String> multivalueMap,
@MatrixVariable(pathVar="cars") MultiValueMap<String, String> mapForPathVar,
@MatrixVariable("name") Map<String, String> mapWithName) {
}
@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);
}
@Override
public boolean supportsParameter(MethodParameter parameter) {
if (!parameter.hasParameterAnnotation(MatrixVariable.class)) {
return false;
}
if (Map.class.isAssignableFrom(parameter.nestedIfOptional().getNestedParameterType())) {
MatrixVariable matrixVariable = parameter.getParameterAnnotation(MatrixVariable.class);
return (matrixVariable != null && StringUtils.hasText(matrixVariable.name()));
}
return true;
}
@RequestMapping("/{root}")
public void handle(@PathVariable("root") int root, @MatrixVariable(required=false, defaultValue="7") int q,
Writer writer) throws IOException {
assertEquals("Invalid path variable value", 42, root);
writer.write("test-" + root + "-" + q);
}
@RequestMapping("/hotels/{hotel}/bookings/{booking}-{other}")
public void handle(@PathVariable("hotel") String hotel,
@PathVariable int booking,
@PathVariable String other,
@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);
writer.write("test-" + hotel + "-q" + qHotel + "-" + booking + "-" + other + "-q" + qOther);
}
@RequestMapping("/hotels/{hotel}/bookings/{booking}-{other}")
public void handle(@PathVariable("hotel") String hotel, @PathVariable int booking,
@PathVariable String other, @MatrixVariable MultiValueMap<String, String> params) {
assertEquals("Invalid path variable value", "42", hotel);
assertEquals("Invalid path variable value", 21, booking);
assertEquals(Arrays.asList("1", "2", "3"), params.get("q"));
assertEquals("R", params.getFirst("r"));
}
@RequestMapping("/{root:\\d+}{params}")
public void handle(@PathVariable("root") int root, @PathVariable("params") String paramString,
@MatrixVariable List<Integer> q, Writer writer) throws IOException {
assertEquals("Invalid path variable value", 42, root);
writer.write("test-" + root + "-" + paramString + "-" + q);
}
@SuppressWarnings("unused")
public void handle(
String stringArg,
@MatrixVariable Map<String, String> map,
@MatrixVariable MultiValueMap<String, String> multivalueMap,
@MatrixVariable(pathVar="cars") MultiValueMap<String, String> mapForPathVar,
@MatrixVariable("name") Map<String, String> mapWithName) {
}
@Override
public boolean test(MethodParameter parameter) {
MatrixVariable annotation = parameter.getParameterAnnotation(MatrixVariable.class);
return annotation != null &&
(this.name == null || this.name.equalsIgnoreCase(annotation.name())) &&
(this.pathVar == null || this.pathVar.equalsIgnoreCase(annotation.pathVar()));
}
@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);
}
@GetMapping("/{one}/{two}/{three}-{four}")
public String matrixVar(
@MatrixVariable int p,
@MatrixVariable(name = "q", pathVar = "two") int q2,
@MatrixVariable(name = "q", pathVar = "four") int q4) {
return "p=" + p + ", q2=" + q2 + ", q4=" + q4;
}
@SuppressWarnings("unused")
void handle(
String stringArg,
@MatrixVariable Map<String, String> map,
@MatrixVariable MultiValueMap<String, String> multivalueMap,
@MatrixVariable(pathVar="cars") MultiValueMap<String, String> mapForPathVar,
@MatrixVariable("name") Map<String, String> mapWithName) {
}
@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);
}
@Override
public boolean supportsParameter(MethodParameter parameter) {
if (!parameter.hasParameterAnnotation(MatrixVariable.class)) {
return false;
}
if (Map.class.isAssignableFrom(parameter.nestedIfOptional().getNestedParameterType())) {
MatrixVariable matrixVariable = parameter.getParameterAnnotation(MatrixVariable.class);
return (matrixVariable != null && StringUtils.hasText(matrixVariable.name()));
}
return true;
}
@RequestMapping("/{root}")
public void handle(@PathVariable("root") int root, @MatrixVariable(required=false, defaultValue="7") int q,
Writer writer) throws IOException {
assertEquals("Invalid path variable value", 42, root);
writer.write("test-" + root + "-" + q);
}
@RequestMapping("/hotels/{hotel}/bookings/{booking}-{other}")
public void handle(@PathVariable("hotel") String hotel,
@PathVariable int booking,
@PathVariable String other,
@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);
writer.write("test-" + hotel + "-q" + qHotel + "-" + booking + "-" + other + "-q" + qOther);
}
@RequestMapping("/hotels/{hotel}/bookings/{booking}-{other}")
public void handle(@PathVariable("hotel") String hotel, @PathVariable int booking,
@PathVariable String other, @MatrixVariable MultiValueMap<String, String> params) {
assertEquals("Invalid path variable value", "42", hotel);
assertEquals("Invalid path variable value", 21, booking);
assertEquals(Arrays.asList("1", "2", "3"), params.get("q"));
assertEquals("R", params.getFirst("r"));
}
@RequestMapping("/{root:\\d+}{params}")
public void handle(@PathVariable("root") int root, @PathVariable("params") String paramString,
@MatrixVariable List<Integer> q, Writer writer) throws IOException {
assertEquals("Invalid path variable value", 42, root);
writer.write("test-" + root + "-" + paramString + "-" + q);
}
@SuppressWarnings("unused")
public void handle(
String stringArg,
@MatrixVariable Map<String, String> map,
@MatrixVariable MultiValueMap<String, String> multivalueMap,
@MatrixVariable(pathVar="cars") MultiValueMap<String, String> mapForPathVar,
@MatrixVariable("name") Map<String, String> mapWithName) {
}
@Override
public boolean test(MethodParameter parameter) {
MatrixVariable annotation = parameter.getParameterAnnotation(MatrixVariable.class);
return annotation != null &&
(this.name == null || this.name.equalsIgnoreCase(annotation.name())) &&
(this.pathVar == null || this.pathVar.equalsIgnoreCase(annotation.pathVar()));
}
@Override
public boolean supportsParameter(MethodParameter parameter) {
MatrixVariable matrixVariable = parameter.getParameterAnnotation(MatrixVariable.class);
if (matrixVariable != null) {
if (Map.class.isAssignableFrom(parameter.getParameterType())) {
return !StringUtils.hasText(matrixVariable.name());
}
}
return false;
}
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest request, 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<String, String>();
String pathVariable = parameter.getParameterAnnotation(MatrixVariable.class).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()) {
for (String name : vars.keySet()) {
for (String value : vars.get(name)) {
map.add(name, value);
}
}
}
}
return (isSingleValueMap(parameter) ? map.toSingleValueMap() : map);
}
@Override
public boolean supportsParameter(MethodParameter parameter) {
if (!parameter.hasParameterAnnotation(MatrixVariable.class)) {
return false;
}
if (Map.class.isAssignableFrom(parameter.nestedIfOptional().getNestedParameterType())) {
String variableName = parameter.getParameterAnnotation(MatrixVariable.class).name();
return StringUtils.hasText(variableName);
}
return true;
}
@Override
public boolean supportsParameter(MethodParameter parameter) {
MatrixVariable matrixVariable = parameter.getParameterAnnotation(MatrixVariable.class);
if (matrixVariable != null) {
if (Map.class.isAssignableFrom(parameter.getParameterType())) {
return !StringUtils.hasText(matrixVariable.name());
}
}
return false;
}
@Override
public boolean supportsParameter(MethodParameter parameter) {
if (!parameter.hasParameterAnnotation(MatrixVariable.class)) {
return false;
}
if (Map.class.isAssignableFrom(parameter.getParameterType())) {
String variableName = parameter.getParameterAnnotation(MatrixVariable.class).name();
return StringUtils.hasText(variableName);
}
return true;
}
@RequestMapping("/{root}")
public void handle(@PathVariable("root") int root, @MatrixVariable(required=false, defaultValue="7") int q,
Writer writer) throws IOException {
assertEquals("Invalid path variable value", 42, root);
writer.write("test-" + root + "-" + q);
}
@RequestMapping("/hotels/{hotel}/bookings/{booking}-{other}")
public void handle(@PathVariable("hotel") String hotel,
@PathVariable int booking,
@PathVariable String other,
@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);
writer.write("test-" + hotel + "-q" + qHotel + "-" + booking + "-" + other + "-q" + qOther);
}