矩阵变量
基本概念
在 Spring MVC / Spring Boot 中,矩阵变量(Matrix Variable) 指的是:URL 路径中的键值参数使用 ; 分隔,而不是 ?。
普通 URL 参数:
/cars?color=red&year=2020矩阵变量写法:
/cars;color=red;year=2020矩阵变量还支持逗号分隔值:
/cars/tesla;color=red,blue,black@MatrixVariable
- 矩阵变量是单值
URL:
/cars/tesla;color=red;year=2020Controller:
java
@GetMapping("/cars/{brand}")
public String cars(
@PathVariable String brand,
@MatrixVariable String color,
@MatrixVariable Integer year) {
System.out.println(brand);
System.out.println(color);
System.out.println(year);
return "ok";
}- 矩阵变量是多值
URL:
/cars/tesla;color=red,blue,blackController:
java
@GetMapping("/cars/{path}")
public String test(
@MatrixVariable List<String> color) {
return "ok";
}