日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

玩轉Spring MVC自定義請求匹配規則

來源: 責編: 時間:2023-12-04 09:21:14 240觀看
導讀環境:SpringBoot2.7.12前言在Spring MVC框架中,HandlerMapping是用于將HTTP請求映射到處理器的方法的組件。當一個請求到達時,HandlerMapping會根據請求的URL和其他屬性來確定哪個處理器方法應該處理該請求。在Spring MV

環境:SpringBoot2.7.12eYH28資訊網——每日最新資訊28at.com

前言

在Spring MVC框架中,HandlerMapping是用于將HTTP請求映射到處理器的方法的組件。當一個請求到達時,HandlerMapping會根據請求的URL和其他屬性來確定哪個處理器方法應該處理該請求。在Spring MVC中,我們可以自定義HandlerMapping來滿足特定的匹配需求。其中一個方法是使用getCustomMethodCondition()方法來自定義匹配條件。eYH28資訊網——每日最新資訊28at.com

本文將詳細介紹如何使用getCustomMethodCondition()方法來自定義HandlerMapping的匹配條件。通過閱讀本文,您將了解如何擴展HandlerMapping的默認行為,并使用自定義條件來匹配請求和處理器方法。eYH28資訊網——每日最新資訊28at.com

需求:我們希望根據請求header中的x-token值來匹配具體的接口。所有的接口都必須使用了自定義的注解標注。eYH28資訊網——每日最新資訊28at.com

1. 自定義請求匹配

在SpringMVC中可以通過自定義RequestMappingHandlerMapping#getCustomMethodCondition來實現此功能。eYH28資訊網——每日最新資訊28at.com

自定義請求匹配通過實現RequestCondition接口自定義規則eYH28資訊網——每日最新資訊28at.com

系統默認提供了以下RequestCondition實現eYH28資訊網——每日最新資訊28at.com

圖片圖片eYH28資訊網——每日最新資訊28at.com

2. 自定義匹配條件

public class CustomRequestCondition implements RequestCondition<CustomRequestCondition> {  private static final String X_TOKEN_NAME = "x-token" ;  private Method method ;  public CustomRequestCondition(Method method) {    this.method = method ;  }  // 當接口上有多個匹配規則時,進行合并操作  @Override  public CustomRequestCondition combine(CustomRequestCondition other) {    return new CustomRequestCondition(other.method) ;  }  // 核心方法:根據匹配的條件進行判斷是否匹配,如果匹配則返回當前的對象,不匹配則返回null  @Override  public CustomRequestCondition getMatchingCondition(HttpServletRequest request) {    AKF akf = method.getAnnotation(AKF.class) ;    return akf != null ? buildToken(request, akf) : null ;  }  // 當有多個都滿足條件的時候,進行比較具體使用哪個  @Override  public int compareTo(CustomRequestCondition other, HttpServletRequest request) {    return 0 ;  }  // 判斷請求header中的信息與注解中配置的信息是否一致  private CustomRequestCondition buildToken(HttpServletRequest request, AKF akf) {    String xToken = request.getHeader(X_TOKEN_NAME) ;    if (xToken == null || xToken.length() == 0) {      return null ;    }    return xToken.equals(akf.value()) ? this : null ;  }}

3. 配置自定義HandlerMapping

public class CustomMethodConditionRequestHandlerMapping extends RequestMappingHandlerMapping {  @Override  protected RequestCondition<?> getCustomMethodCondition(Method method) {    return new CustomRequestCondition(method) ;  }}

配置自定義的HandlerMappingeYH28資訊網——每日最新資訊28at.com

@Componentpublic class CustomEndpointConfig implements WebMvcRegistrations {  public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {    return new CustomMethodConditionRequestHandlerMapping() ;  }}

通過實現WebMvcRegistrations中的getRequestMappingHandlerMapping方法覆蓋系統默認的RequestMappingHandlerMapping配置實現。當然這種方式你可能失去了某些功能。這里我們可以參考默認實現來完善自定義的實現。eYH28資訊網——每日最新資訊28at.com

4. 測試接口

@RestController@RequestMapping("/conditions")public class CustomMethodConditionController {  @GetMapping("/index")  public Object index() {    return "custom method condition success" ;  }  @GetMapping("/index")  @AKF  public Object x() {    return "x method invoke" ;  }  @GetMapping("/index")  @AKF("x1")  public Object x1() {    return "x1 method invoke" ;  }  @GetMapping("/index")  @AKF("x2")  public Object x2() {    return "x2 method invoke" ;  }}

上面的接口與通常的開發配置是一致的,只是有些有接口使用了@AKF注解。這些接口中,沒有@AKF注解或者沒有設置@AKF值的,都不能訪問,只有設置值了,且請求中攜帶了x-token并匹配上值了才會訪問到接口。eYH28資訊網——每日最新資訊28at.com

圖片eYH28資訊網——每日最新資訊28at.com

當訪問其它沒有@AKF注解的接口,返回404。eYH28資訊網——每日最新資訊28at.com

5. 原理

根據請求查找HandlerMethodeYH28資訊網——每日最新資訊28at.com

public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMapping {  protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {    String lookupPath = initLookupPath(request);    try {      // 根據請求查找匹配d餓HandlerMethod      HandlerMethod handlerMethod = lookupHandlerMethod(lookupPath, request);      return (handlerMethod != null ? handlerMethod.createWithResolvedBean() : null);    }  }  protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {    List<Match> matches = new ArrayList<>();    // 根據請求的uri,獲取相應的RequestMappingInfo(該對象對應的Controller中的每一個接口)    List<T> directPathMatches = this.mappingRegistry.getMappingsByDirectPath(lookupPath);    if (directPathMatches != null) {      // 根據請求找到了相應的RequestMappingInfo,則進行匹配執行相應的條件      addMatchingMappings(directPathMatches, matches, request);    }    // ...  }  private void addMatchingMappings(Collection<T> mappings, List<Match> matches, HttpServletRequest request) {    for (T mapping : mappings) {      // 執行相應的條件進行匹配,比如:你在@RequestMapping中配置了header,params等相應的值      T match = getMatchingMapping(mapping, request);      if (match != null) {        matches.add(new Match(match, this.mappingRegistry.getRegistrations().get(mapping)));      }    }  }}public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMethodMapping<RequestMappingInfo> {  protected RequestMappingInfo getMatchingMapping(RequestMappingInfo info, HttpServletRequest request) {    return info.getMatchingCondition(request);  }}// RequestMappingInfopublic final class RequestMappingInfo {  // 該方法中就會根據請求request對象,判斷是否當前對象符合條件  public RequestMappingInfo getMatchingCondition(HttpServletRequest request) {    RequestMethodsRequestCondition methods = this.methodsCondition.getMatchingCondition(request);    if (methods == null) {      return null;    }    ParamsRequestCondition params = this.paramsCondition.getMatchingCondition(request);    if (params == null) {      return null;    }    HeadersRequestCondition headers = this.headersCondition.getMatchingCondition(request);    if (headers == null) {      return null;    }    // ...    // 我們配置了自定義的,這里就會執行我們自定義的條件(必須有@AKF注解)    RequestConditionHolder custom = this.customConditionHolder.getMatchingCondition(request);    if (custom == null) {      // 返回null 則表示當前的RequestMappingInfo沒有匹配。      // 最終如果都是返回的null,則最終返回客戶端將是404      return null;    }    return new RequestMappingInfo(this.name, pathPatterns, patterns,        methods, params, headers, consumes, produces, custom, this.options);  }}

在本文中,介紹了如何自定義RequestMappingHandlerMapping。通過自定義getCustomMethodCondition()方法,我們可以根據特定的需求擴展HandlerMapping的行為,并使用自定義條件來匹配請求和處理器方法。通過這種方式,我們可以更好地控制請求的處理邏輯。eYH28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-37264-0.html玩轉Spring MVC自定義請求匹配規則

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 慢聊Golang的websocket使用和實現代碼分析

下一篇: 什么是軟件架構需要演進的時機,你懂嗎?

標簽:
  • 熱門焦點
  • 官方承諾:K60至尊版將會首批升級MIUI 15

    全新的MIUI 15今天也有了消息,在官宣了K60至尊版將會搭載天璣9200+處理器和獨顯芯片X7的同時,Redmi給出了官方承諾,K60至尊重大更新首批升級,會首批推送MIUI 15。也就是說雖然
  • 俄羅斯:將審查iPhone等外國公司設備 保數據安全

    iPhone和特斯拉都屬于在各自領域領頭羊的品牌,推出的產品也也都是數一數二的,但對于一些國家而言,它們的產品可靠性和安全性還是在限制范圍內。近日,俄羅斯聯邦通信、信息技術
  • 小米官宣:2023年上半年出貨量中國第一!

    今日早間,小米電視官方微博帶來消息,稱2023年小米電視上半年出貨量達到了中國第一,同時還表示小米電視的巨屏風暴即將開始。“公布一個好消息2023年#小米電視上半年出貨量中國
  • 學習JavaScript的10個理由...

    作者 | Simplilearn編譯 | 王瑞平當你決心學習一門語言的時候,很難選擇到底應該學習哪一門,常用的語言有Python、Java、JavaScript、C/CPP、PHP、Swift、C#、Ruby、Objective-
  • 梁柱接棒兩年,騰訊音樂闖出新路子

    文丨田靜 出品丨牛刀財經(niudaocaijing)7月5日,企鵝FM發布官方公告稱由于業務調整,將于9月6日正式停止運營,這意味著騰訊音樂長音頻業務走向消亡。騰訊在長音頻領域還在摸索。為
  • 當家的盒馬,加速謀生

    來源 | 價值星球Planet作者 | 歸去來自己&ldquo;當家&rdquo;的盒馬,開始加速謀生了。據盒馬官微消息,盒馬計劃今年開放生鮮供應鏈,將其生鮮商品送往食堂。目前,盒馬在上海已經與
  • OPPO K11搭載長壽版100W超級閃充:26分鐘充滿100%

    據此前官方宣布,OPPO將于7月25日也就是今天下午14:30舉辦新品發布會,屆時全新的OPPO K11將正式與大家見面,將主打旗艦影像,和同檔位競品相比,其最大的賣
  • OPPO K11搭載高性能石墨散熱系統:旗艦同款 性能涼爽釋放

    日前OPPO官方宣布,將于7月25日14:30舉辦新品發布會,屆時全新的OPPO K11將正式與大家見面,將主打旗艦影像,和同檔位競品相比,其最大的賣點就是將配備索尼
  • 榮耀Magic4 至臻版 首創智慧隱私通話 強勁影音系統

    2022年第一季度臨近尾聲,在該季度內,許多品牌陸續發布自己的最新產品,讓大家從全新的角度來了解當今的手機技術。手機是電子設備中,更新迭代十分迅速的一款產品,基
Top 主站蜘蛛池模板: 大关县| 固阳县| 资源县| 卓资县| 萍乡市| 长岛县| 韶山市| 天气| 鹤庆县| 西乌珠穆沁旗| 绥宁县| 定安县| 涿鹿县| 西林县| 海宁市| 泸水县| 高清| 郸城县| 洛宁县| 秦安县| 太保市| 景德镇市| 凤凰县| 沅江市| 丁青县| 阜阳市| 商都县| 乌鲁木齐市| 庆云县| 滨州市| 新乐市| 通江县| 江西省| 彭阳县| 上高县| 靖远县| 桂东县| 宁明县| 旺苍县| 榆社县| 旌德县|