动态注册 spring 控制器的路由
问题:
如何在 spring 中动态注册控制器的路由,而不指定显式参数类型?
回答:
要动态注册控制器路由,可以使用 java 反射机制来获取方法的参数类型。下面是更新的代码:
package dry.example.service.impl;
import java.lang.reflect.method;
...
private class> getparametertype(object handler, string methodname) throws nosuchmethodexception {
method method = handler.getcl
ass().getmethod(methodname);
return method.getparametertypes()[0];
}通过反射动态获取参数类型后,可以在代码中动态注入。
RequestMappingInfo requestMappingInfo = RequestMappingInfo.paths("testing").methods(RequestMethod.GET).build();
Method method = handler.getClass().getMethod("h01", getParameterType(handler, "h01"));
requestMappingHandlerMapping.registerMapping(requestMappingInfo, handler, method);








