Spring MVC高級知識點:自定義請求匹配路徑
環(huán)境:springboot2.2.10.RELEASE
自定義請求匹配
希望根據(jù)請求中header['x-token']的不同值調(diào)用不同的接口。接口請求地址相同,根據(jù)不同的header信息調(diào)用不同的執(zhí)行方法。
在SpringMVC中可以通過自定義
RequestMappingHandlerMapping#getCustomMethodCondition來實現(xiàn)此功能。
自定義請求匹配通過實現(xiàn)RequestCondition接口自定義規(guī)則
系統(tǒng)默認提供了以下RequestCondition實現(xiàn)

自定義匹配條件
- 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 ;
- }
- // 當接口上有多個匹配規(guī)則時,進行合并操作
- @Override
- public CustomRequestCondition combine(CustomRequestCondition other) {
- return new CustomRequestCondition(other.method) ;
- }
- // 核心方法:根據(jù)匹配的條件進行判斷是否匹配,如果匹配則返回當前的對象,不匹配則返回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 ;
- }
- }
自定義HandlerMapping
- public class CustomMethodConditionRequestHandlerMapping extends RequestMappingHandlerMapping {
- @Override
- protected RequestCondition<?> getCustomMethodCondition(Method method) {
- return new CustomRequestCondition(method) ;
- }
- }
配置自定義的HandlerMapping
- @Configuration
- public class CustomEndpointConfig extends WebMvcConfigurationSupport {
- @Override
- protected RequestMappingHandlerMapping createRequestMappingHandlerMapping() {
- return new CustomMethodConditionRequestHandlerMapping() ;
- }
- }
注冊HandlerMapping我們也可以通過@Bean的方式,但是這種方式會使得你在定義多個相同接口地址的時候容器啟動就會報錯
而且@Bean的方式是向容器中注冊一個HandlerMapping對象;而通過上面的方式就是替換系統(tǒng)默認的
RequestMappingHandlerMapping對象。兩種方式是不一樣的,一個是增加一個HandlerMapping,一個是替換系統(tǒng)默認的。
測試接口
- @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" ;
- }
- }
上面的接口地址完全相關(guān),只是有些有@AKF注解,有些沒有。
如果通過@Bean注冊一個HandlerMapping后,多個請求路徑相同會報如下錯誤:
- Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'customMethodConditionController' method
- com.pack.controller.CustomMethodConditionController#x()
- to {GET /conditions/index}: There is already 'customMethodConditionController' bean method
- com.pack.controller.CustomMethodConditionController#index() mapped.
- at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.validateMethodMapping(AbstractHandlerMethodMapping.java:636) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
- at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:603) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
在以上的請求中如果請求中沒有攜帶x-token信息或者是value值不被匹配那么請求會是404。