您现在的位置是:亿华云 > 应用开发
Spring MVC高级知识点:自定义请求匹配路径
亿华云2025-10-09 11:06:25【应用开发】9人已围观
简介环境:springboot2.2.10.RELEASE自定义请求匹配希望根据请求中header[x-token]的不同值调用不同的接口。接口请求地址相同,根据不同的header信息调用不同的执行方法。
环境:springboot2.2.10.RELEASE
自定义请求匹配
希望根据请求中header[x-token]的知径不同值调用不同的接口。接口请求地址相同,识点根据不同的自定header信息调用不同的执行方法。
在SpringMVC中可以通过自定义
RequestMappingHandlerMapping#getCustomMethodCondition来实现此功能。义请
自定义请求匹配通过实现RequestCondition接口自定义规则
系统默认提供了以下RequestCondition实现

自定义匹配条件
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中的云南idc服务商信息与注解中配置的信息是否一致 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对象;而通过上面的方式就是替换系统默认的
RequestMappingHandlerMapping对象。两种方式是求匹不一样的,免费信息发布网一个是配路增加一个HandlerMapping,一个是知径替换系统默认的。
测试接口
@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注解,有些没有。
如果通过@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。
香港云服务器很赞哦!(2232)
相关文章
- 当投资者经过第二阶段的认真学习之后又充满了信心,认为自己可以在市场上叱咤风云地大干一场了。但没想到“看花容易绣花难”,由于对理论知识不会灵活运用.从而失去灵活应变的本能,就经常会出现小赢大亏的局面,结果往往仍以失败告终。这使投资者很是困惑和痛苦,不知该如何办,甚至开始怀疑这个市场是不是不适合自己。在这种情况下,有的人选择了放弃,但有的意志坚定者则决定做最后的尝试。
- 关于网上程序员接私活的问题
- 利用Python写属于自己的翻译命令行
- 手把手:我的深度学习模型训练好了,然后要做啥?
- 互联网中的地址是数字的IP地址,域名解析的作用主要就是为了便于记忆。
- 2017年编程语言排行榜:PHP仅第8名,Java第3!
- 初识Python:Hello World和字符串操作
- 使用Python和dlib进行人脸检测
- 为了避免将来给我们的个人站长带来的麻烦,在选择域名后缀时,我们的站长最好省略不稳定的后缀域名,比如n,因为我们不知道策略什么时候会改变,更不用说我们将来是否还能控制这个域名了。因此,如果站长不是企业,或者有选择的话,如果不能选择域名的cn类,最好不要选择它。
- 从输入url开始能做哪些优化