在Spring3.2 後,一個@MatrixVariable出現了,這個註解的出現拓展了URL請求地址的功能。
如果要開啓Matrix Variable功能的話,必須設置 RequestMappingHandlerMapping 中的 removeSemicolonContent 爲false.
一般情況不用你手動去設置這個屬性,因爲這個屬性默認就是false ,如果你碰見Matrix Variable功能未開啓的時候就可以看看是不是誤設置這個屬性爲true了。
Springboot 默認是無法使用矩陣變量綁定參數的。需要覆蓋WebMvcConfigurer中的configurePathMatch方法。
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper=new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}
Demo
// http://127.0.0.1:9082/sys/v1/count/test/id35;price=100;type=1,3/msg/msg44
// http://127.0.0.1:9082/sys/v1/count/test/id35;price=100;type=1,3/msg/msg44;vol=100,45
@GetMapping("test/{id}/msg/{tip}")
//public String testMatrix(@MatrixVariable(pathVar = "id") Map<String, List<String>> map, @PathVariable("tip") String tip){
//public String testMatrix(@MatrixVariable Map<String, List<String>> map, @PathVariable("tip") String tip){
public String testMatrix(@MatrixVariable(pathVar = "id") Map<String, List<String>> map, @MatrixVariable(value = "vol", pathVar = "tip") List<String> list, @PathVariable("id") String id, @PathVariable("tip") String tip){
System.out.println("id:" + id);
System.out.println("tip:" + tip);
System.out.println("map:" + map);
System.out.println("list:" + list);
return "";
}
//http://127.0.0.1:9082/sys/v1/count/tablet;low=100;hight=1000?manufacturer=google
@RequestMapping("/{category}")
public String filterProducts(
@PathVariable("category") String category,
@MatrixVariable Map<String, List<String>> filterParams,
@RequestParam("manufacturer") String manufacturer, Model model) {
return "";
}
//http://127.0.0.1:9082/sys/v1/count/tablet/low=100;hight=1000?manufacturer=google
Matrix Variable中,多個變量可以使用“;”(分號)分隔,例如: 按照類別,價格區間,生產者查詢,其實最後一個查詢條件使用@RequestParam,類別使用@PathVariable,真正使用到matrixVariable的是 價格區間。 http://127.0.0.1:9082/sys/v1/count/tablet/low=100;hight=1000?manufacturer=google @RequestMapping("/{category}/{ByCriteria}") public String filterProducts( @PathVariable("category") String category, @MatrixVariable(pathVar="ByCriteria") Map<String, List<String>> filterParams, @RequestParam("manufacturer") String manufacturer, Model model) {} // GET /owners/42;q=11/pets/21;q=22 @RequestMapping(value = "/owners/{ownerId}/pets/{petId}", method = RequestMethod.GET) public voidfindPet( @MatrixVariable(value="q", pathVar="ownerId") intq1, @MatrixVariable(value="q", pathVar="petId") intq2) { // q1 == 11 // q2 == 22 //spring3.2.3 } 針對每一個Parh Variable綁定一個Matrix Variable,然後使用 value 和 pathVar屬性就能找到該值。 另外,正對Matrix Variable也是可以指定自身的的屬性,例如,是否必須,默認值。 下面這個例子說明: Java代碼 收藏代碼 // GET /pets/42 @RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET) public voidfindPet(@MatrixVariable(required=true, defaultValue="1") intq) { // q == 1 } 根據 品牌,類別查詢代碼如下 http://xxx/xxxpr/filter/ByCriteria;brand=dell,google;category=tablet,laptop @RequestMapping("/filter/{ByCriteria}") public String getProductsByFilter(@MatrixVariable(pathVar="ByCriteria") Map<String, List<String>> filterParams, Model model) {} 如果是一個變量的多個值那麼可以使用“,”(逗號)分隔 Java代碼 收藏代碼 color=red,green,blue 或者可以使用重複的變量名: Java代碼 收藏代碼 color=red;color=green;color=blue