反射获得springmvc的所有方法,入参出参

本文介绍如何通过反射技术深入探索SpringMVC框架,详细解析如何获取控制器中的所有方法及其输入输出参数,帮助理解框架底层工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@RequestMapping("/test")
public void test(HttpSession session) throws Exception {
	//获取所有被RestController注解的类
	WebApplicationContext wc = WebApplicationContextUtils.getRequiredWebApplicationContext(session.getServletContext());
	Map<String, Object> beansWithAnnotation = wc.getBeansWithAnnotation(RestController.class);
	Set<Map.Entry<String, Object>> entries = beansWithAnnotation.entrySet();

	//遍历集合
	for (Map.Entry<String, Object> entry : entries) {
		Object value = entry.getValue();
		Class<?> clazz = value.getClass();
		Method[] methods = clazz.getMethods();
		//获取类上的RequestMapping的值
		RequestMapping controllerMapping = clazz.getAnnotation(RequestMapping.class);
		String controllerUrl = springPath+ controllerMapping.value()[0];
		for (Method method : methods) {
			boolean present = method.isAnnotationPresent(RequestMapping.class);
			if (present) {
				RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
				ApiOperation annotation = method.getAnnotation(ApiOperation.class);
				String annotationValue = annotation.value();
				LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer();
				//获得方法上的所有所有入参名
				String[] parameterNames = discoverer.getParameterNames(method);
				Class<?>[] parameterTypes = method.getParameterTypes();
				String param = "";
				for (int i =0; i < parameterTypes.length;i++) {
					ClassLoader classLoader = parameterTypes[i].getClassLoader();
					if (classLoader == null) {
						//如果参数是基本类
						param += parameterTypes[i].getSimpleName() + " " + parameterNames[i] +",";

					} else {
						//如果参数是封装类
						Field[] fields = parameterTypes[i].getDeclaredFields();
						for (Field field : fields) {
							param += field.getType().getSimpleName() + " " + field.getName() +",";
						}
					}
				}

				//获取方法的返回参数类型
				String returnCallback = "";
				Type genericReturnType = method.getGenericReturnType();
				if (genericReturnType instanceof ParameterizedType) { // 判断获取的类型是否是参数类型
					Type[] typesto = ((ParameterizedType) genericReturnType).getActualTypeArguments();// 强制转型为带参数的泛型类型,
					Type type = typesto[0];
					String typeName = type.getTypeName();
					if (type instanceof ParameterizedType) {
						typeName = typeName.substring(0,typeName.indexOf("<"));
						typeName = typeName.substring(typeName.lastIndexOf(".")+1);
						returnCallback += "<"+typeName+"<";
						Type[] typesto2 = ((ParameterizedType) type).getActualTypeArguments();
						for (int i =0; i < typesto2.length; i++) {
							Type type1 = typesto2[i];
							String typeName1 = type1.getTypeName();
							if (typeName1.indexOf("<") >= 0) {
								typeName1 = typeName1.substring(0,typeName1.indexOf("<"));
								typeName1 = typeName1.substring(typeName1.lastIndexOf(".")+1);
							} else {
								typeName1 = typeName1.substring(typeName1.lastIndexOf(".")+1);
							}
							if (i == typesto2.length -1) returnCallback += typeName1;
							else returnCallback += typeName1+",";
						}
						returnCallback += ">>";

					} else {
						returnCallback += "<"+typeName.substring(typeName.lastIndexOf(".")+1)+">";
					}

				}

			}
		}

	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值