
spring mvc 动态添加 RequestMapping
spring mvc 动态添加 RequestMapping
想法总是非常独特,有时候我们想在运行期间动态添加接口,那么就用到了动态添加 RequestMapping了
首先我们得搞清楚 springmvc 请求原理,客户端发起请求,会先去 RequestMappingHandlerMapping 里面去查找,如果找不到,就 404 了。
明白了请求原理,那么动态添加就容易了。只需要往RequestMappingHandlerMapping里面添加一个请求映射就可以了。
AbstractHandlerMethodMapping 提供了 registerMapping 和 unregisterMapping 方法
继承关系:
RequestMappingHandlerMapping -> RequestMappingInfoHandlerMapping -> AbstractHandlerMethodMapping
具体代码:
@Autowired RequestMappingHandlerMapping requestMappingHandlerMapping; public ResultData hello(){ return ResultData.success("hello"); } @PostMapping("get") @ResponseBody public ResultData addRequestMapper() throws IllegalAccessException, InstantiationException { Class<?> entry = this.getClass(); // hello 是方法名,ReflectionUtils 是 org.springframework.util 包 Method methodName = ReflectionUtils.findMethod(entry, "hello"); PatternsRequestCondition patterns = new PatternsRequestCondition("op/api/test/hello"); RequestMethodsRequestCondition method = new RequestMethodsRequestCondition(RequestMethod.POST); RequestMappingInfo mappingInfo = new RequestMappingInfo(patterns, method, null, null, null, null, null); requestMappingHandlerMapping.registerMapping(mappingInfo, entry.newInstance(), methodName); return ResultData.success(); }👁️ 阅读量:0
© 版权声明:本文《spring mvc 动态添加 RequestMapping》内容均为本站精心整理或网友自愿分享,如需转载请注明原文出处:https://www.zastudy.cn/wen/1686619402a307265.html。