欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot通過@MatrixVariable進(jìn)行傳參詳解

 更新時(shí)間:2022年06月27日 09:56:36   作者:鳴鼓ming  
這篇文章主要介紹了SpringBoot使用@MatrixVariable傳參,文章圍繞@MatrixVariable展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下

1.相關(guān)概念

語法: 請(qǐng)求路徑:/person/info;name=lisi;hobbies=basketball,football,tennis不同變量用分號(hào)相隔, 一個(gè)變量有多個(gè)值則使用逗號(hào)隔開

SpringBoot默認(rèn)是禁用了矩陣變量的功能

手動(dòng)開啟原理: 對(duì)于路徑的處理, UrlPathHelper的removeSemicolonContent設(shè)置為false,讓其支持矩陣變量的。

矩陣變量必須有url路徑變量才能被解析, 也就是/person/{path}里的path(這里我把它的值寫成info, 即/person/info)

2.開啟矩陣變量

第一種方法,實(shí)現(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è)置不移除分號(hào) ;后面的內(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è)置不移除分號(hào) ;后面的內(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MyBatis深入解讀動(dòng)態(tài)SQL的實(shí)現(xiàn)

    MyBatis深入解讀動(dòng)態(tài)SQL的實(shí)現(xiàn)

    動(dòng)態(tài) SQL 是 MyBatis 的強(qiáng)大特性之一。如果你使用過 JDBC 或其它類似的框架,你應(yīng)該能理解根據(jù)不同條件拼接 SQL 語句有多痛苦,例如拼接時(shí)要確保不能忘記添加必要的空格,還要注意去掉列表最后一個(gè)列名的逗號(hào)。利用動(dòng)態(tài) SQL,可以徹底擺脫這種痛苦
    2022-04-04
  • Java面向?qū)ο箨P(guān)鍵字extends繼承的深入講解

    Java面向?qū)ο箨P(guān)鍵字extends繼承的深入講解

    繼承就是使用已定義的類作為父類,新建一個(gè)類作為子類使用extends關(guān)鍵字繼承這個(gè)類,這樣就實(shí)現(xiàn)了繼承關(guān)系,這篇文章主要給大家介紹了關(guān)于Java面向?qū)ο箨P(guān)鍵字extends繼承的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • java基于quasar實(shí)現(xiàn)協(xié)程池的方法示例

    java基于quasar實(shí)現(xiàn)協(xié)程池的方法示例

    本文主要介紹了java基于quasar實(shí)現(xiàn)協(xié)程池的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>
    2022-06-06
  • 相冊(cè)管理系統(tǒng)(Java表單+xml數(shù)據(jù)庫存儲(chǔ))

    相冊(cè)管理系統(tǒng)(Java表單+xml數(shù)據(jù)庫存儲(chǔ))

    這篇文章主要為大家詳細(xì)介紹了相冊(cè)管理系統(tǒng)的實(shí)現(xiàn)步驟,Java表單的文件上傳和下載,xml數(shù)據(jù)庫存儲(chǔ)信息,感興趣的小伙伴們可以參考一下
    2016-07-07
  • 使用mybatis格式化查詢出的日期

    使用mybatis格式化查詢出的日期

    這篇文章主要介紹了使用mybatis格式化查詢出的日期,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • springboot文件虛擬路徑映射方式

    springboot文件虛擬路徑映射方式

    這篇文章主要介紹了springboot文件虛擬路徑映射方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 防止SpringMVC攔截器攔截js等靜態(tài)資源文件的解決方法

    防止SpringMVC攔截器攔截js等靜態(tài)資源文件的解決方法

    本篇文章主要介紹了防止SpringMVC攔截器攔截js等靜態(tài)資源文件的解決方法,具有一定的參考價(jià)值,有興趣的同學(xué)可以了解一下
    2017-09-09
  • Mybatis源碼分析之插件模塊

    Mybatis源碼分析之插件模塊

    今天給大家?guī)淼氖顷P(guān)于Mybatis的相關(guān)知識(shí),文章圍繞著Mybatis插件模塊展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Java數(shù)據(jù)結(jié)構(gòu)與算法之樹(動(dòng)力節(jié)點(diǎn)java學(xué)院整理)

    Java數(shù)據(jù)結(jié)構(gòu)與算法之樹(動(dòng)力節(jié)點(diǎn)java學(xué)院整理)

    這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)與算法之樹的相關(guān)知識(shí),最主要的是二叉樹中的二叉搜索樹,需要的朋友可以參考下
    2017-04-04
  • java DecimalFormat常用方法詳解

    java DecimalFormat常用方法詳解

    這篇文章主要為大家詳細(xì)介紹了java DecimalFormat的常用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03

最新評(píng)論