获取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("调用失败");
}
}
- 获取RequestMappingHandlerMapping中的URL映射
- spring中的url模板映射
- springMvc中的Url模板映射
- 获取URL中的信息
- 获取url中的文件名
- url中的域名获取
- 获取 url 中的参数
- Spring获取已映射的URL列表
- asp.net2.0中的URL映射
- asp.net 2.0中的url映射
- ASP.NET 2.0中的URL映射
- [翻译]asp.net 2.0中的url映射
- Servlet映射到多个URL中的一些问题
- [翻译]asp.net 2.0中的url映射
- web.xml中的url-pattern映射规则
- web.xml中的url-pattern映射规则
- web.xml中的url-pattern映射规则
- web.xml中的url-pattern映射规则
- WiFi和热点开发
- Mac:xampp 无法启动apache问题
- java 内存分配与回收
- 跨部门的高效沟通与协作
- 超声波模块学习
- 获取RequestMappingHandlerMapping中的URL映射
- make -C $(LINUX_KERNEL_PATH) M=$(PWD) modules中的M选项
- php 怎么实现弱类型
- 从云办公感受到的科学技术为我们带来的便利
- Centos 7.2 安装 Mysql 5.7.13
- javascript中的原型/对象
- hdu 1548 A strange lift
- Ant编译多个依赖工程jar、war包
- 欢迎使用CSDN-markdown编辑器