您好,登錄后才能下訂單哦!
這篇文章主要介紹“SpringBoot怎么自定義路由覆蓋”的相關(guān)知識,小編通過實(shí)際案例向大家展示操作過程,操作方法簡單快捷,實(shí)用性強(qiáng),希望這篇“SpringBoot怎么自定義路由覆蓋”文章能幫助大家解決問題。
公司最近有一個(gè)項(xiàng)目二期需要對一些功能進(jìn)行改造,涉及部分框架內(nèi)置業(yè)務(wù)接口個(gè)性化定制,兼容老接口功能并且增加一部分新的數(shù)據(jù)返回,由于前端調(diào)用這些接口分布較多且較為零碎,修改測試成本較大,所以打算在框架層面提供路由覆蓋功能,加快項(xiàng)目進(jìn)度減少無技術(shù)含量的修改帶來的系統(tǒng)風(fēng)險(xiǎn)
提供自定義注解指定需要覆蓋的路由及新路由地址
系統(tǒng)啟動(dòng)時(shí)掃描所有注解數(shù)據(jù)并進(jìn)行映射處理
注冊自定義路由映射配置類
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface CoverRoute { String value() default ""; }
在系統(tǒng)啟動(dòng)時(shí)調(diào)用initRoute方法,把原路由和對應(yīng)的覆蓋路由映射到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() + "個(gè)"); } } public static boolean checkExistCover(String orginalRoute) { return mappingRegist.containsKey(orginalRoute); } public static String getRedirectRoute(String orginalRoute) { return mappingRegist.get(orginalRoute); } }
繼承RequestMappingHandlerMapping重寫lookupHandlerMethod方法,在spring進(jìn)行路由尋址時(shí)進(jìn)行覆蓋
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); } } }
@Component public class WebRequestMappingConfig implements WebMvcRegistrations { public RequestMappingHandlerMapping getRequestMappingHandlerMapping() { RequestMappingHandlerMapping handlerMapping = new CustomRequestMappingHandlerMapping(); handlerMapping.setOrder(0); return handlerMapping; } }
在個(gè)性化接口類增加@CoverRoute注解,指定需要覆蓋的路由地址,創(chuàng)建相同路由路徑的的方法即可,訪問原來的接口地址會(huì)自動(dòng)轉(zhuǎn)發(fā)到項(xiàng)目個(gè)性化接口地址
原接口
@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; } }
關(guān)于“SpringBoot怎么自定義路由覆蓋”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。