SpringBoot通過@MatrixVariable進行傳參詳解
1.相關(guān)概念
語法: 請求路徑:/person/info;name=lisi;hobbies=basketball,football,tennis不同變量用分號相隔, 一個變量有多個值則使用逗號隔開
SpringBoot默認是禁用了矩陣變量的功能
手動開啟原理: 對于路徑的處理, 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進行傳參詳解的文章就介紹到這了,更多相關(guān)SpringBoot @MatrixVariable內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis深入解讀動態(tài)SQL的實現(xiàn)
動態(tài) SQL 是 MyBatis 的強大特性之一。如果你使用過 JDBC 或其它類似的框架,你應(yīng)該能理解根據(jù)不同條件拼接 SQL 語句有多痛苦,例如拼接時要確保不能忘記添加必要的空格,還要注意去掉列表最后一個列名的逗號。利用動態(tài) SQL,可以徹底擺脫這種痛苦2022-04-04Java面向?qū)ο箨P(guān)鍵字extends繼承的深入講解
繼承就是使用已定義的類作為父類,新建一個類作為子類使用extends關(guān)鍵字繼承這個類,這樣就實現(xiàn)了繼承關(guān)系,這篇文章主要給大家介紹了關(guān)于Java面向?qū)ο箨P(guān)鍵字extends繼承的相關(guān)資料,需要的朋友可以參考下2021-08-08java基于quasar實現(xiàn)協(xié)程池的方法示例
本文主要介紹了java基于quasar實現(xiàn)協(xié)程池的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>2022-06-06相冊管理系統(tǒng)(Java表單+xml數(shù)據(jù)庫存儲)
這篇文章主要為大家詳細介紹了相冊管理系統(tǒng)的實現(xiàn)步驟,Java表單的文件上傳和下載,xml數(shù)據(jù)庫存儲信息,感興趣的小伙伴們可以參考一下2016-07-07防止SpringMVC攔截器攔截js等靜態(tài)資源文件的解決方法
本篇文章主要介紹了防止SpringMVC攔截器攔截js等靜態(tài)資源文件的解決方法,具有一定的參考價值,有興趣的同學(xué)可以了解一下2017-09-09Java數(shù)據(jù)結(jié)構(gòu)與算法之樹(動力節(jié)點java學(xué)院整理)
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)與算法之樹的相關(guān)知識,最主要的是二叉樹中的二叉搜索樹,需要的朋友可以參考下2017-04-04