av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

Spring MVC高級(jí)知識(shí)點(diǎn):自定義請(qǐng)求匹配路徑

環(huán)境:springboot2.2.10.RELEASE

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

希望根據(jù)請(qǐng)求中header['x-token']的不同值調(diào)用不同的接口。接口請(qǐng)求地址相同,根據(jù)不同的header信息調(diào)用不同的執(zhí)行方法。

在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)

自定義匹配條件

 
 
 
 
  1. public class CustomRequestCondition implements RequestCondition { 
  2.      
  3.     private static final String X_TOKEN_NAME = "x-token" ; 
  4.  
  5.     private Method method ; 
  6.      
  7.     public CustomRequestCondition(Method method) { 
  8.         this.method = method ; 
  9.     } 
  10.      
  11.     // 當(dāng)接口上有多個(gè)匹配規(guī)則時(shí),進(jìn)行合并操作 
  12.     @Override 
  13.     public CustomRequestCondition combine(CustomRequestCondition other) { 
  14.         return new CustomRequestCondition(other.method) ; 
  15.     } 
  16.  
  17.     // 核心方法:根據(jù)匹配的條件進(jìn)行判斷是否匹配,如果匹配則返回當(dāng)前的對(duì)象,不匹配則返回null 
  18.     @Override 
  19.     public CustomRequestCondition getMatchingCondition(HttpServletRequest request) { 
  20.         AKF akf = method.getAnnotation(AKF.class) ; 
  21.         return akf != null ? buildToken(request, akf) : null ; 
  22.     } 
  23.  
  24.     // 當(dāng)有多個(gè)都滿足條件的時(shí)候,進(jìn)行比較具體使用哪個(gè) 
  25.     @Override 
  26.     public int compareTo(CustomRequestCondition other, HttpServletRequest request) { 
  27.         return 0 ; 
  28.     } 
  29.      
  30.     // 判斷請(qǐng)求header中的信息與注解中配置的信息是否一致 
  31.     private CustomRequestCondition buildToken(HttpServletRequest request, AKF akf) { 
  32.         String xToken = request.getHeader(X_TOKEN_NAME) ; 
  33.         if (xToken == null || xToken.length() == 0) { 
  34.             return null ; 
  35.         } 
  36.         return xToken.equals(akf.value()) ? this : null ; 
  37.     } 
  38.  

自定義HandlerMapping

 
 
 
 
  1. public class CustomMethodConditionRequestHandlerMapping extends RequestMappingHandlerMapping { 
  2.     @Override 
  3.     protected RequestCondition getCustomMethodCondition(Method method) { 
  4.         return new CustomRequestCondition(method) ; 
  5.     } 

配置自定義的HandlerMapping

 
 
 
 
  1. @Configuration 
  2. public class CustomEndpointConfig extends WebMvcConfigurationSupport { 
  3.      
  4.     @Override 
  5.     protected RequestMappingHandlerMapping createRequestMappingHandlerMapping() { 
  6.         return new CustomMethodConditionRequestHandlerMapping() ; 
  7.     } 
  8.      

注冊(cè)HandlerMapping我們也可以通過(guò)@Bean的方式,但是這種方式會(huì)使得你在定義多個(gè)相同接口地址的時(shí)候容器啟動(dòng)就會(huì)報(bào)錯(cuò)

而且@Bean的方式是向容器中注冊(cè)一個(gè)HandlerMapping對(duì)象;而通過(guò)上面的方式就是替換系統(tǒng)默認(rèn)的

RequestMappingHandlerMapping對(duì)象。兩種方式是不一樣的,一個(gè)是增加一個(gè)HandlerMapping,一個(gè)是替換系統(tǒng)默認(rèn)的。

測(cè)試接口

 
 
 
 
  1. @RestController 
  2. @RequestMapping("/conditions") 
  3. public class CustomMethodConditionController { 
  4.      
  5.     @GetMapping("/index") 
  6.     public Object index() { 
  7.         return "custom method condition success" ; 
  8.     } 
  9.      
  10.     @GetMapping("/index") 
  11.     @AKF 
  12.     public Object x() { 
  13.         return "x method invoke" ; 
  14.     } 
  15.      
  16.     @GetMapping("/index") 
  17.     @AKF("x1") 
  18.     public Object x1() { 
  19.         return "x1 method invoke" ; 
  20.     } 
  21.      
  22.     @GetMapping("/index") 
  23.     @AKF("x2") 
  24.     public Object x2() { 
  25.         return "x2 method invoke" ; 
  26.     } 
  27.      

上面的接口地址完全相關(guān),只是有些有@AKF注解,有些沒(méi)有。

如果通過(guò)@Bean注冊(cè)一個(gè)HandlerMapping后,多個(gè)請(qǐng)求路徑相同會(huì)報(bào)如下錯(cuò)誤:

 
 
 
 
  1. Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'customMethodConditionController' method  
  2. com.pack.controller.CustomMethodConditionController#x() 
  3. to {GET /conditions/index}: There is already 'customMethodConditionController' bean method 
  4. com.pack.controller.CustomMethodConditionController#index() mapped. 
  5.     at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.validateMethodMapping(AbstractHandlerMethodMapping.java:636) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE] 
  6.     at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:603) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE] 

在以上的請(qǐng)求中如果請(qǐng)求中沒(méi)有攜帶x-token信息或者是value值不被匹配那么請(qǐng)求會(huì)是404。


分享文章:Spring MVC高級(jí)知識(shí)點(diǎn):自定義請(qǐng)求匹配路徑
鏈接分享:http://uogjgqi.cn/article/ccosgso.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們?cè)谖⑿派?4小時(shí)期待你的聲音

解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流