亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

在本文中,介紹了如何自定義RequestMAppingHandlerMapping。通過(guò)自定義getCustomMethodCondition()方法,我們可以根據(jù)特定的需求擴(kuò)展HandlerMapping的行為,并使用自定義條件來(lái)匹配請(qǐng)求和處理器方法。通過(guò)這種方式,我們可以更好地控制請(qǐng)求的處理邏輯。

環(huán)境:SpringBoot2.7.12

前言

在Spring MVC框架中,HandlerMapping是用于將HTTP請(qǐng)求映射到處理器的方法的組件。當(dāng)一個(gè)請(qǐng)求到達(dá)時(shí),HandlerMapping會(huì)根據(jù)請(qǐng)求的URL和其他屬性來(lái)確定哪個(gè)處理器方法應(yīng)該處理該請(qǐng)求。在Spring MVC中,我們可以自定義HandlerMapping來(lái)滿足特定的匹配需求。其中一個(gè)方法是使用getCustomMethodCondition()方法來(lái)自定義匹配條件。

本文將詳細(xì)介紹如何使用getCustomMethodCondition()方法來(lái)自定義HandlerMapping的匹配條件。通過(guò)閱讀本文,您將了解如何擴(kuò)展HandlerMapping的默認(rèn)行為,并使用自定義條件來(lái)匹配請(qǐng)求和處理器方法。

需求:我們希望根據(jù)請(qǐng)求header中的x-token值來(lái)匹配具體的接口。所有的接口都必須使用了自定義的注解標(biāo)注。

1. 自定義請(qǐng)求匹配

在SpringMVC中可以通過(guò)自定義RequestMappingHandlerMapping#getCustomMethodCondition來(lái)實(shí)現(xiàn)此功能。

自定義請(qǐng)求匹配通過(guò)實(shí)現(xiàn)RequestCondition接口自定義規(guī)則

系統(tǒng)默認(rèn)提供了以下RequestCondition實(shí)現(xiàn)

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

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)接口上有多個(gè)匹配規(guī)則時(shí),進(jìn)行合并操作
  @Override
  public CustomRequestCondition combine(CustomRequestCondition other) {
    return new CustomRequestCondition(other.method) ;
  }


  // 核心方法:根據(jù)匹配的條件進(jìn)行判斷是否匹配,如果匹配則返回當(dāng)前的對(duì)象,不匹配則返回null
  @Override
  public CustomRequestCondition getMatchingCondition(HttpServletRequest request) {
    AKF akf = method.getAnnotation(AKF.class) ;
    return akf != null ? buildToken(request, akf) : null ;
  }


  // 當(dāng)有多個(gè)都滿足條件的時(shí)候,進(jìn)行比較具體使用哪個(gè)
  @Override
  public int compareTo(CustomRequestCondition other, HttpServletRequest request) {
    return 0 ;
  }


  // 判斷請(qǐng)求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) ;
  }
}

配置自定義的HandlerMapping

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

通過(guò)實(shí)現(xiàn)WebMvcRegistrations中的getRequestMappingHandlerMapping方法覆蓋系統(tǒng)默認(rèn)的RequestMappingHandlerMapping配置實(shí)現(xiàn)。當(dāng)然這種方式你可能失去了某些功能。這里我們可以參考默認(rèn)實(shí)現(xiàn)來(lái)完善自定義的實(shí)現(xiàn)。

4. 測(cè)試接口

@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" ;
  }
}

上面的接口與通常的開(kāi)發(fā)配置是一致的,只是有些有接口使用了@AKF注解。這些接口中,沒(méi)有@AKF注解或者沒(méi)有設(shè)置@AKF值的,都不能訪問(wèn),只有設(shè)置值了,且請(qǐng)求中攜帶了x-token并匹配上值了才會(huì)訪問(wèn)到接口。

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

當(dāng)訪問(wèn)其它沒(méi)有@AKF注解的接口,返回404。

5. 原理

根據(jù)請(qǐng)求查找HandlerMethod

public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMapping {
  protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
    String lookupPath = initLookupPath(request);
    try {
      // 根據(jù)請(qǐng)求查找匹配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ù)請(qǐng)求的uri,獲取相應(yīng)的RequestMappingInfo(該對(duì)象對(duì)應(yīng)的Controller中的每一個(gè)接口)
    List<T> directPathMatches = this.mappingRegistry.getMappingsByDirectPath(lookupPath);
    if (directPathMatches != null) {
      // 根據(jù)請(qǐng)求找到了相應(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);
  }
}
// RequestMappingInfo
public final class RequestMappingInfo {
  // 該方法中就會(huì)根據(jù)請(qǐng)求request對(duì)象,判斷是否當(dāng)前對(duì)象符合條件
  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;
    }


    // ...
    // 我們配置了自定義的,這里就會(huì)執(zhí)行我們自定義的條件(必須有@AKF注解)
    RequestConditionHolder custom = this.customConditionHolder.getMatchingCondition(request);
    if (custom == null) {
      // 返回null 則表示當(dāng)前的RequestMappingInfo沒(méi)有匹配。
      // 最終如果都是返回的null,則最終返回客戶端將是404
      return null;
    }
    return new RequestMappingInfo(this.name, pathPatterns, patterns,
        methods, params, headers, consumes, produces, custom, this.options);
  }
}

在本文中,介紹了如何自定義RequestMappingHandlerMapping。通過(guò)自定義getCustomMethodCondition()方法,我們可以根據(jù)特定的需求擴(kuò)展HandlerMapping的行為,并使用自定義條件來(lái)匹配請(qǐng)求和處理器方法。通過(guò)這種方式,我們可以更好地控制請(qǐng)求的處理邏輯。

分享到:
標(biāo)簽:Spring
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定