栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Controller的@RequestMapping拼接父类Controller@RequestMapping的Path

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Controller的@RequestMapping拼接父类Controller@RequestMapping的Path

  1. 开发项目过程中有个需求是业务Controller的@RequestMapping上都有"/api/v1"这种共有的Path,以后也有可能变成"/api/v2"、"/api/vn"等。所以想写个BaseController,在BaseController上标注"/api/v1",业务Controller继承BaseController,项目启动时自动把BaseController上的Path追加到业务Controller的Path前面。
  2. 实现过程,重写RequestMappingHandlerMapping的getMappingForMethod方法,将BaseController上的Path追加上去。实现WebMvcRegistrations接口将自己的RequestMappingHandlerMapping注册。直接上代码
  3. import org.springframework.core.annotation.AnnotatedElementUtils;
    import org.springframework.lang.Nullable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.mvc.condition.RequestCondition;
    import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
    import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
    
    import java.lang.reflect.AnnotatedElement;
    import java.lang.reflect.Method;
    
    
    public class RequestMappingHandlerMappingWrapping extends RequestMappingHandlerMapping {
    
        @Override
        protected RequestMappingInfo getMappingForMethod(Method method, Class handlerType) {
            RequestMappingInfo mappingInfo = super.getMappingForMethod(method, handlerType);
            Class superClass = handlerType.getSuperclass();
            mappingInfo = appendSuperRequestMapping(superClass,mappingInfo);
            return mappingInfo;
        }
    
        
        protected RequestMappingInfo appendSuperRequestMapping(Class handlerType,RequestMappingInfo mappingInfo) {
            if(handlerType == null || mappingInfo == null) {
                return mappingInfo;
            }
            RequestMapping superRequestMapping = handlerType.getAnnotation(RequestMapping.class);
            if(superRequestMapping != null && superRequestMapping.value().length > 0) {
                RequestMappingInfo typeInfo = this.createRequestMappingInfo(handlerType);
                if (typeInfo != null) {
                    mappingInfo = typeInfo.combine(mappingInfo);
                }
            }
            return appendSuperRequestMapping(handlerType.getSuperclass(),mappingInfo);
        }
    
        @Nullable
        private RequestMappingInfo createRequestMappingInfo(AnnotatedElement element) {
            RequestMapping requestMapping = (RequestMapping) AnnotatedElementUtils.findMergedAnnotation(element, RequestMapping.class);
            RequestCondition condition = element instanceof Class ? this.getCustomTypeCondition((Class)element) : this.getCustomMethodCondition((Method)element);
            return requestMapping != null ? this.createRequestMappingInfo(requestMapping, condition) : null;
        }
    
    }
    import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.Ordered;
    import org.springframework.core.annotation.Order;
    import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
    
    
    
    @Configuration
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public class WebMvcConfiguration implements WebMvcRegistrations {
    
        @Override
        public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
            return new RequestMappingHandlerMappingWrapping();
        }
    
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/894010.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号