乐趣区

关于springboot:SpringBoot实现自定义路由覆盖

背景

公司最近有一个我的项目二期须要对一些性能进行革新,波及局部框架内置业务接口个性化定制,兼容老接口性能并且减少一部分新的数据返回,因为前端调用这些接口散布较多且较为系统,批改测试老本较大,所以打算在框架层面提供路由笼罩性能,放慢我的项目进度缩小无技术含量的批改带来的零碎危险

设计

  • 提供自定义注解指定须要笼罩的路由及新路由地址
  • 系统启动时扫描所有注解数据并进行映射解决
  • 注册自定义路由映射配置类

实现

注解定义

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface CoverRoute {String value() default "";
}

注解扫描及治理

在系统启动时调用 initRoute 办法,把原路由和对应的笼罩路由映射到 map 键值对中

public class ConverRouteUtil {private static HashMap<String, String> mappingRegist = new HashMap<>();

    public static void initRoute(Class runtimeClass, List<String> extraPackageNameList) {List<Class<?>> scanClassList = new ArrayList<>();
        if (!runtimeClass.getPackage().getName().equals(Application.class.getPackage().getName())) {scanClassList.addAll(ScanUtil.getAllClassByPackageName_Annotation(runtimeClass.getPackage(), CoverRoute.class));
        }
        for (String packageName : extraPackageNameList) {scanClassList.addAll(ScanUtil.getAllClassByPackageName_Annotation(packageName, CoverRoute.class));
        }
        for (Class clazz : scanClassList) {CoverRoute coverRoute = (CoverRoute) clazz.getAnnotation(CoverRoute.class);
            if (StringUtil.isEmpty(coverRoute.value())) {continue;}
            RequestMapping requestMapping = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
            String classRoute = "";
            if (requestMapping != null) {classRoute = requestMapping.value()[0];
            } else {continue;}
            List<Method> methodList = Arrays.asList(clazz.getDeclaredMethods());
            for (Method method : methodList) {PostMapping postMapping = method.getAnnotation(PostMapping.class);
                String methodRoute = "";
                if (postMapping != null) {methodRoute = postMapping.value()[0];
                } else {GetMapping getMapping = method.getAnnotation(GetMapping.class);
                    if (getMapping != null) {methodRoute = getMapping.value()[0];
                    }
                }
                if (!StringUtil.isEmpty(classRoute) && !StringUtil.isEmpty(methodRoute)) {String orginalRoute = coverRoute.value() + methodRoute;
                    String redirectRoute = classRoute + methodRoute;
                    mappingRegist.put(orginalRoute, redirectRoute);
                }
            }
        }
        if (mappingRegist.size() > 0) {System.out.println("扫描路由办法笼罩:" + mappingRegist.size() + "个");
        }
    }

    public static boolean checkExistCover(String orginalRoute) {return mappingRegist.containsKey(orginalRoute);
    }

    public static String getRedirectRoute(String orginalRoute) {return mappingRegist.get(orginalRoute);
    }
}

自定义 RequestMappingHandlerMapping

继承 RequestMappingHandlerMapping 重写 lookupHandlerMethod 办法,在 spring 进行路由寻址时进行笼罩

public class CustomRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
    @Override
    protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {if(ConverRouteUtil.checkExistCover(lookupPath)){String redirectRoute = ConverRouteUtil.getRedirectRoute(lookupPath);
            request.setAttribute("redirectTag","1");
            request.setAttribute("redirectRoute",redirectRoute);
            request.setAttribute("lookupPath",lookupPath);
            lookupPath = redirectRoute;
        }else{request.setAttribute("redirectTag","0");
        }
        return super.lookupHandlerMethod(lookupPath, request);
    }

    @Override
    protected RequestMappingInfo getMatchingMapping(RequestMappingInfo info, HttpServletRequest request) {String redirectTag = ConvertOp.convert2String(request.getAttribute("redirectTag"));
        if(redirectTag.equals("1")){String redirectRoute = ConvertOp.convert2String(request.getAttribute("redirectRoute"));
            boolean check = false;
            if(info.getPatternsCondition()!=null){Set<String> set =  info.getPatternsCondition().getPatterns();
                if(set.size()>0){String[] array = new String[set.size()];
                    array = set.toArray(array);
                    String pattern = array[0];
                    if(pattern.equals(redirectRoute)){check = true;}
                }
            }
            if(check){return info;}else{return super.getMatchingMapping(info, request);
            }
        }else{return super.getMatchingMapping(info, request);
        }
    }
}

注册 RequestMappingHandlerMapping

@Component
public class WebRequestMappingConfig implements WebMvcRegistrations {public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {RequestMappingHandlerMapping handlerMapping = new CustomRequestMappingHandlerMapping();
        handlerMapping.setOrder(0);
        return handlerMapping;
    }

}

应用示例

在个性化接口类减少 @CoverRoute 注解,指定须要笼罩的路由地址,创立雷同路由门路的的办法即可,拜访原来的接口地址会主动转发到我的项目个性化接口地址

  • 原接口
@Controller
@RequestMapping("/example/original")
public class RedirectOriginalExampleController {@PostMapping("/getConfig")
    @ResponseBody
    @AnonymousAccess
    public Object getConfig(@RequestBody Map<String, Object> params) {Result result = Result.okResult();
        result.add("tag","original");
        return result;
    }
}
  • 新接口
@Controller
@RequestMapping("/example/redirect")
@CoverRoute("/example/original")
public class RedirectExampleController {@PostMapping("/getConfig")
    @ResponseBody
    public Object getConfig(@RequestBody Map<String, Object> params) {Result result = Result.okResult();
        String param1 = ConvertOp.convert2String(params.get("param1"));
        result.add("tag","redirect");
        result.add("param1",param1);
        return result;
    }
}
退出移动版