玩转Spring MVC自定义请求匹配规则

开发 前端
在本文中,介绍了如何自定义RequestMappingHandlerMapping。通过自定义getCustomMethodCondition()方法,我们可以根据特定的需求扩展HandlerMapping的行为,并使用自定义条件来匹配请求和处理器方法。通过这种方式,我们可以更好地控制请求的处理逻辑。

环境:SpringBoot2.7.12

前言

在Spring MVC框架中,HandlerMapping是用于将HTTP请求映射到处理器的方法的组件。当一个请求到达时,HandlerMapping会根据请求的URL和其他属性来确定哪个处理器方法应该处理该请求。在Spring MVC中,我们可以自定义HandlerMapping来满足特定的匹配需求。其中一个方法是使用getCustomMethodCondition()方法来自定义匹配条件。

本文将详细介绍如何使用getCustomMethodCondition()方法来自定义HandlerMapping的匹配条件。通过阅读本文,您将了解如何扩展HandlerMapping的默认行为,并使用自定义条件来匹配请求和处理器方法。

需求:我们希望根据请求header中的x-token值来匹配具体的接口。所有的接口都必须使用了自定义的注解标注。

1. 自定义请求匹配

在SpringMVC中可以通过自定义RequestMappingHandlerMapping#getCustomMethodCondition来实现此功能。

自定义请求匹配通过实现RequestCondition接口自定义规则

系统默认提供了以下RequestCondition实现

图片图片

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) ;
  }
}

配置自定义的HandlerMapping

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

通过实现WebMvcRegistrations中的getRequestMappingHandlerMapping方法覆盖系统默认的RequestMappingHandlerMapping配置实现。当然这种方式你可能失去了某些功能。这里我们可以参考默认实现来完善自定义的实现。

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并匹配上值了才会访问到接口。

图片

当访问其它没有@AKF注解的接口,返回404。

5. 原理

根据请求查找HandlerMethod

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);
  }
}
// RequestMappingInfo
public 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的行为,并使用自定义条件来匹配请求和处理器方法。通过这种方式,我们可以更好地控制请求的处理逻辑。

责任编辑:武晓燕 来源: Spring全家桶实战案例源码
相关推荐

2021-08-13 08:36:15

SpringMVC自定义

2016-08-23 13:21:15

MVC路由视图

2023-12-28 08:22:33

响应数据转换

2022-11-10 07:53:54

Spring参数校验

2023-03-26 08:41:37

2022-11-01 11:15:56

接口策略模式

2009-11-24 15:11:21

ASP.NET MVC

2009-07-22 15:27:39

ASP.NET MVC自定义路由

2015-02-12 15:33:43

微信SDK

2011-03-17 09:45:01

Spring

2010-04-30 09:32:49

ASP.NET MVC

2015-02-12 15:38:26

微信SDK

2017-08-03 17:00:54

Springmvc任务执行器

2022-06-20 08:26:39

Spring容器类型转换

2020-11-25 11:20:44

Spring注解Java

2016-12-26 15:25:59

Android自定义View

2016-11-16 21:55:55

源码分析自定义view androi

2011-06-23 10:49:13

Qt 自定义信号

2022-06-27 08:16:34

JSON格式序列化

2021-05-12 08:32:53

Spring Secu 自定义session
点赞
收藏

51CTO技术栈公众号