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

當(dāng)前位置:首頁 > 科技  > 軟件

玩轉(zhuǎn)Spring MVC自定義請求匹配規(guī)則

來源: 責(zé)編: 時間:2023-12-04 09:21:14 208觀看
導(dǎo)讀環(huán)境:SpringBoot2.7.12前言在Spring MVC框架中,HandlerMapping是用于將HTTP請求映射到處理器的方法的組件。當(dāng)一個請求到達(dá)時,HandlerMapping會根據(jù)請求的URL和其他屬性來確定哪個處理器方法應(yīng)該處理該請求。在Spring MV

環(huán)境:SpringBoot2.7.126wm28資訊網(wǎng)——每日最新資訊28at.com

前言

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

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

需求:我們希望根據(jù)請求header中的x-token值來匹配具體的接口。所有的接口都必須使用了自定義的注解標(biāo)注。6wm28資訊網(wǎng)——每日最新資訊28at.com

1. 自定義請求匹配

在SpringMVC中可以通過自定義RequestMappingHandlerMapping#getCustomMethodCondition來實現(xiàn)此功能。6wm28資訊網(wǎng)——每日最新資訊28at.com

自定義請求匹配通過實現(xiàn)RequestCondition接口自定義規(guī)則6wm28資訊網(wǎng)——每日最新資訊28at.com

系統(tǒng)默認(rèn)提供了以下RequestCondition實現(xiàn)6wm28資訊網(wǎng)——每日最新資訊28at.com

圖片圖片6wm28資訊網(wǎng)——每日最新資訊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 ;  }  // 當(dāng)接口上有多個匹配規(guī)則時,進(jìn)行合并操作  @Override  public CustomRequestCondition combine(CustomRequestCondition other) {    return new CustomRequestCondition(other.method) ;  }  // 核心方法:根據(jù)匹配的條件進(jìn)行判斷是否匹配,如果匹配則返回當(dāng)前的對象,不匹配則返回null  @Override  public CustomRequestCondition getMatchingCondition(HttpServletRequest request) {    AKF akf = method.getAnnotation(AKF.class) ;    return akf != null ? buildToken(request, akf) : null ;  }  // 當(dāng)有多個都滿足條件的時候,進(jìn)行比較具體使用哪個  @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) ;  }}

配置自定義的HandlerMapping6wm28資訊網(wǎng)——每日最新資訊28at.com

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

通過實現(xiàn)WebMvcRegistrations中的getRequestMappingHandlerMapping方法覆蓋系統(tǒng)默認(rèn)的RequestMappingHandlerMapping配置實現(xiàn)。當(dāng)然這種方式你可能失去了某些功能。這里我們可以參考默認(rèn)實現(xiàn)來完善自定義的實現(xiàn)。6wm28資訊網(wǎng)——每日最新資訊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" ;  }}

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

圖片6wm28資訊網(wǎng)——每日最新資訊28at.com

當(dāng)訪問其它沒有@AKF注解的接口,返回404。6wm28資訊網(wǎng)——每日最新資訊28at.com

5. 原理

根據(jù)請求查找HandlerMethod6wm28資訊網(wǎng)——每日最新資訊28at.com

public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMapping {  protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {    String lookupPath = initLookupPath(request);    try {      // 根據(jù)請求查找匹配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<>();    // 根據(jù)請求的uri,獲取相應(yīng)的RequestMappingInfo(該對象對應(yīng)的Controller中的每一個接口)    List<T> directPathMatches = this.mappingRegistry.getMappingsByDirectPath(lookupPath);    if (directPathMatches != null) {      // 根據(jù)請求找到了相應(yīng)的RequestMappingInfo,則進(jìn)行匹配執(zhí)行相應(yīng)的條件      addMatchingMappings(directPathMatches, matches, request);    }    // ...  }  private void addMatchingMappings(Collection<T> mappings, List<Match> matches, HttpServletRequest request) {    for (T mapping : mappings) {      // 執(zhí)行相應(yīng)的條件進(jìn)行匹配,比如:你在@RequestMapping中配置了header,params等相應(yīng)的值      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 {  // 該方法中就會根據(jù)請求request對象,判斷是否當(dāng)前對象符合條件  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;    }    // ...    // 我們配置了自定義的,這里就會執(zhí)行我們自定義的條件(必須有@AKF注解)    RequestConditionHolder custom = this.customConditionHolder.getMatchingCondition(request);    if (custom == null) {      // 返回null 則表示當(dāng)前的RequestMappingInfo沒有匹配。      // 最終如果都是返回的null,則最終返回客戶端將是404      return null;    }    return new RequestMappingInfo(this.name, pathPatterns, patterns,        methods, params, headers, consumes, produces, custom, this.options);  }}

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

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

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

上一篇: 慢聊Golang的websocket使用和實現(xiàn)代碼分析

下一篇: 什么是軟件架構(gòu)需要演進(jìn)的時機(jī),你懂嗎?

標(biāo)簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 洛宁县| 会理县| 张家口市| 鄯善县| 许昌市| 资源县| 天祝| 天长市| 平山县| 肥乡县| 清丰县| 衢州市| 台东县| 惠东县| 正镶白旗| 志丹县| 荔波县| 浙江省| 教育| 苍南县| 大新县| 麦盖提县| 和林格尔县| 竹北市| 正定县| 天镇县| 铜陵市| 金华市| 阜城县| 晴隆县| 银川市| 陇西县| 洛南县| 雷波县| 延津县| 运城市| 揭东县| 白山市| 靖西县| 寿宁县| 桂平市|