首頁 > 軟體

SpringBoot自定義路由覆蓋實現流程詳解

2023-01-04 14:00:19

背景

公司最近有一個專案二期需要對一些功能進行改造,涉及部分框架內建業務介面個性化客製化,相容老介面功能並且增加一部分新的資料返回,由於前端呼叫這些介面分佈較多且較為零碎,修改測試成本較大,所以打算在框架層面提供路由覆蓋功能,加快專案進度減少無技術含量的修改帶來的系統風險

設計

  • 提供自定義註解指定需要覆蓋的路由及新路由地址
  • 系統啟動時掃描所有註解資料並進行對映處理
  • 註冊自定義路由對映設定類

實現

註解定義

@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;
    }
}

到此這篇關於SpringBoot自定義路由覆蓋實現流程詳解的文章就介紹到這了,更多相關SpringBoot自定義路由覆蓋內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com