工具类系列-RequestRedirectUtil

来源:互联网 发布:mac怎么下photoshop 编辑:程序博客网 时间:2024/05/22 15:03

  

public static ResponseVo redirect(Class<?> controllerClazz, String methodName, Object arg) {

        if (controllerClazz == null || StringUtils.isBlank(methodName)) {
            throw new IllegalArgumentException("Argument invalid ...");
        }

        Method machedMethod = searchMachedMethod(controllerClazz, methodName);
        
        return doRedirect(controllerClazz, machedMethod, arg);

    }


    /**
     * 根据方法名查找第一个匹配的方法对象
     * @param controllerClazz
     * @param methodName
     * @return
     */
    private static Method searchMachedMethod(Class<?> controllerClazz, String methodName) {
        Method[] methods = controllerClazz.getDeclaredMethods();
        if (ArrayUtils.isEmpty(methods)) {
            throw new NullPointerException("No method found by: " + methodName);
        }
        
        for (Method method : methods) {
            if (StringUtils.equals(methodName, method.getName())) {
                return method;
            }
        }
        
        throw new RuntimeException("No mached method found by: " + methodName);
    }


private static ResponseVo doRedirect(Class<?> controllerClazz, Method method, Object argObject) {
        RequestMapping clazzMapping = controllerClazz.getAnnotation(RequestMapping.class);
        RequestMapping methodMapping = method.getAnnotation(RequestMapping.class);
        if (clazzMapping == null || methodMapping == null) {
            throw new NullPointerException("No @RequestMapping defined for class: " + controllerClazz + " or method: " + method);
        }
        
        String qualifiedUrl = new StringBuilder(baseUrl)
                .append(clazzMapping.value()[0]).append(methodMapping.value()[0]).toString();
        // choose the 1st RequestMethod definition if more than 1 exists
        String requestMethod = methodMapping.method()[0].name();
        String param = JsonUtil.toString(argObject);
        
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("----- redirect 2 TOF via url: " + qualifiedUrl + ",  method: " + requestMethod + ", param: " + param);
        }
        
        String returnStr =XXX;//请求
        
        return JsonUtil.toBean(returnStr, ResponseVo.class);
    }

0 0
原创粉丝点击