SpringBoot通過@MatrixVariable進(jìn)行傳參詳解
1.相關(guān)概念
語法: 請求路徑:/person/info;name=lisi;hobbies=basketball,football,tennis不同變量用分號相隔, 一個變量有多個值則使用逗號隔開
SpringBoot默認(rèn)是禁用了矩陣變量的功能
手動開啟原理: 對于路徑的處理, UrlPathHelper的removeSemicolonContent設(shè)置為false,讓其支持矩陣變量的。
矩陣變量必須有url路徑變量才能被解析, 也就是/person/{path}里的path(這里我把它的值寫成info, 即/person/info)
2.開啟矩陣變量
第一種方法,實現(xiàn)接口WebMvcConfigurer,覆蓋方法configurePathMatch。
WebConfig
package com.limi.springboottest2.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;
@Configuration
public class WebConfig implements WebMvcConfigurer {
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
// 設(shè)置不移除分號 ;后面的內(nèi)容。矩陣變量功能就可以生效
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}第二種,自定義WebMvcConfigurer類型組件并添加到容器中。
WebConfig
package com.limi.springboottest2.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Bean
public WebMvcConfigurer webMvcConfigurer(){
return new WebMvcConfigurer() {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
// 設(shè)置不移除分號 ;后面的內(nèi)容。矩陣變量功能就可以生效
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
};
}
}3.代碼測試
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>歡迎你好呀!</h1>
<a href="/person/info;name=lsi;hobbies=basketball,football,tennis" rel="external nofollow" >1測試@MatrixVariable</a>
<br>
<a href="/person/1;age=50/2;age=30" rel="external nofollow" >2測試@MatrixVariable</a>
</body>
</html>HelloController
package com.limi.springboottest2.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
public class HelloController {
// /person/info;name=lsi;hobbies=basketball,football,tennis
@ResponseBody
@GetMapping("/person/{path}")
public Map<String,Object> get(@PathVariable("path") String path,
@MatrixVariable("name") String name,
@MatrixVariable("hobbies") List<String> hobbies){
Map<String,Object> map = new HashMap<>();
map.put("path",path);
map.put("name",name);
map.put("hobbies",hobbies);
return map;
}
// 測試傳入兩組相同的類型的變量, 例如老板和員工的年齡
// /person/1;age=50/2;age=30
@ResponseBody
@GetMapping("/person/{bossId}/{empId}")
public Map boss(@MatrixVariable(value = "age",pathVar = "bossId") Integer bossAge,
@MatrixVariable(value = "age",pathVar = "empId") Integer empAge){
Map<String,Object> map = new HashMap<>();
map.put("bossAge",bossAge);
map.put("empAge",empAge);
return map;
}
}測試1

測試2

到此這篇關(guān)于SpringBoot通過@MatrixVariable進(jìn)行傳參詳解的文章就介紹到這了,更多相關(guān)SpringBoot @MatrixVariable內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot讀取templates文件html代碼實例
這篇文章主要介紹了Springboot讀取templates文件html代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
Springboot整合freemarker和相應(yīng)的語法詳解
FreeMarker是一款Spring官方推薦使用的模板引擎。接下來通過本文給大家介紹Springboot整合freemarker和相應(yīng)的語法,感興趣的朋友一起看看吧2021-09-09
Java過濾器filter_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了Java過濾器filter,通過過濾器,可以對來自客戶端的請求進(jìn)行攔截,進(jìn)行預(yù)處理或者對最終響應(yīng)給客戶端的數(shù)據(jù)進(jìn)行處理后再輸出2017-07-07
IntelliJ?IDEA教程之clean或者install?Maven項目的操作方法
這篇文章主要介紹了IntelliJ?IDEA教程之clean或者install?Maven項目的操作方法,本文分步驟給大家介紹兩種方式講解如何調(diào)試出窗口,需要的朋友可以參考下2023-04-04
Java手機(jī)號碼工具類示例詳解(判斷運(yùn)營商、獲取歸屬地)
這篇文章主要介紹了Java手機(jī)號碼工具類示例詳解,通過手機(jī)號碼來判斷運(yùn)營商獲取歸屬地,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02

