RestControllerWithRequestMapping简化使用,修改requestMapping

本文探讨如何自定义RequestMapping映射,限制方法范围、避免方法冲突,并实现根据请求URL的version参数控制交易版本。涉及Controller注解解析和AbstractHandlerMethodMapping的扩展。
摘要由CSDN通过智能技术生成


参考
https://my.oschina.net/u/3101282/blog/3022154

在RequestMappingHandlerMapping类中存在判断加载的类是否为转发控制

protected boolean isHandler(Class<?> beanType) {
        return AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) || AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class);
    }


在抽象类AbstractHandlerMethodMapping在processCandidateBean存在调用isHandler
通过detectHandlerMethods获取被注解的类的方法,并注册请求url与方法的映射


尝试自定义过程增加RequestMapping的请求映射
注意点
1,限制映射的方法范围,
    a,只有public的方法才加入,其他作用域不加入,控制范围
    b,针对重载的方法的同名,不允许出现,如重复启动直接异常结束

在后续继续参考
https://blog.csdn.net/philip502/article/details/98502246

真正实现相同requestMap下根据请求url中version参数控制交易版本
该注解适合运用在方法上

继续研究关于类controller的注解


在AbstractHandlerMethodMapping子类初始化后
会通过processCandidateBean方法调用
isHandler判断后,执行detectHandlerMethods
核心逻辑在于
methods.forEach((method, mapping) -> {
                Method invocableMethod = AopUtils.selectInvocableMethod(method, userType);
                this.registerHandlerMethod(handler, invocableMethod, mapping);
            });

但同时需要实现子类RequestMappingHandlerMapping
中getMappingForMethod的类似逻辑,自定义请求过滤的控制交易分发逻辑

结果是可以的,

关键逻辑

private RequestMappingInfo createRequestMappingInfo(AnnotatedElement element) {
    RequestMapping requestMapping = (RequestMapping) AnnotatedElementUtils.findMergedAnnotation(element, RequestMapping.class);

    if (requestMapping != null) {
        RequestCondition<?> condition = element instanceof Class ? this.getCustomTypeCondition((Class) element) : this.getCustomMethodCondition((Method) element);
        return this.createRequestMappingInfo(requestMapping, condition);
    }
    if (element instanceof Method) {
        Method cm = (Method) element;
        if (Modifier.isPublic(cm.getModifiers())) {
            return RequestMappingInfo.paths("/" + cm.getName()).build();
        }
    }

    return null;
}

具体请参见

vic99/RestControllerWithRequestMapping - Gitee.com

后续参考补充,按照lombok的编译时优化,针对指定方法自动加RequestMaping

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值