获取RequestMappingHandlerMapping中的URL映射

来源:互联网 发布:java与c# 编辑:程序博客网 时间:2024/06/05 11:52

使用场景:第三方会调用我发布的接口,但是不管是调用什么接口,都是通过调用一个公共方法,方法名称则是以参数({"call":"order.sendOrder"})形式传递,我需要根据参数找到对应的方法,利用Java反射调用方法。

public class HanderUtil {

public static RequestToMethodItem hander(HttpServletRequest request,String call){
ServletContext servletContext = request.getSession().getServletContext();
    if (servletContext == null)
    {
        return null;
    }
    RequestToMethodItem item =null;
    WebApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);


    //请求url和处理方法的映射
    List<RequestToMethodItem> requestToMethodItemList = new ArrayList<RequestToMethodItem>();
    //获取所有的RequestMapping
    Map<String, HandlerMapping> allRequestMappings = BeanFactoryUtils.beansOfTypeIncludingAncestors(appContext, 
            HandlerMapping.class, true, false);
    
    for (HandlerMapping handlerMapping : allRequestMappings.values())
    {
        //本项目只需要RequestMappingHandlerMapping中的URL映射
        if (handlerMapping instanceof RequestMappingHandlerMapping)
        {
            RequestMappingHandlerMapping requestMappingHandlerMapping = (RequestMappingHandlerMapping) handlerMapping;
            Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
            for (Map.Entry<RequestMappingInfo, HandlerMethod> requestMappingInfoHandlerMethodEntry : handlerMethods.entrySet())
            {
                RequestMappingInfo requestMappingInfo = requestMappingInfoHandlerMethodEntry.getKey();
                HandlerMethod mappingInfoValue = requestMappingInfoHandlerMethodEntry.getValue();
                PatternsRequestCondition patternsCondition = requestMappingInfo.getPatternsCondition();
               if(call.equalsIgnoreCase(patternsCondition.toString())){
               String controllerName = mappingInfoValue.getBeanType().toString().replace("class", "").trim();
                   String requestMethodName = mappingInfoValue.getMethod().getName();
                   Class<?>[] methodParamTypes = mappingInfoValue.getMethod().getParameterTypes();
                   item= new RequestToMethodItem(controllerName, requestMethodName, methodParamTypes);
                   return item;
               }
            }
            break;
        }
    }
return item;
}  
   
public static Result<Object> sendRequest(String call,String args,HttpServletRequest request) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
RequestToMethodItem item=hander(request, call);
if(item!=null){
Class catClass = Class.forName(item.controllerName);
Object obj = catClass.newInstance();
Method[] methods = catClass.getMethods();
    // 循环查找想要的方法
    for(Method method : methods) {
        if(item.methodName.equals(method.getName())) {
            // 调用这个方法,invoke第一个参数是类名,后面是方法需要的参数
        return (Result<Object>) method.invoke(obj, args);
        }
    }

return ResultUtil.error("调用失败");
}

}



原创粉丝点击