springboot动态注册接口

42 阅读1分钟

@Resource
private RequestMappingHandlerMapping handlerMapping;

try {
    Method method = TemplateCtrl.class.getMethod("hello", HttpServletRequest.class);
    RequestMappingInfo requestMappingInfo = RequestMappingInfo.paths("/**").methods(RequestMethod.values()).build();
    handlerMapping.registerMapping(requestMappingInfo, new TemplateCtrl(), method);
} catch (NoSuchMethodException e) {
    e.printStackTrace();
}