参考Spring官方文档说明:https://docs.spring.io/spring-framework/docs/5.3.6/reference/html/web.html#mvc-ann-requestmapping-registration
public void registerMapp注册地ing(RequestMappingInfo mapping, Object handler, Method method) {
super.registerMapping(mapping, handler, method);
this.updateConsumesCondition(mapping, method);
}
实现删除注册信息
public void unregisterMapping(T mapping) {
if (this.logger.isTraceEnabled()) {
this.logger.trace("Unregister mapping "" mapping """);
}
内容 this.mappingRegistry.u资料nregister(mapping);
}
通过这两个方法,我们就可以在工程中方便的编写注册与删除url资源路径的逻辑了
使用方式
1.编写注册/删除用的Controller层方法
示例代码:https://github.com/netbuffer/spring-boot-demo/blob/master/src/main/java/cn/netbuffer/springboot/demo/controller/RequestMappingController.java
/**
* 动态注册路径映射
*
* @param data
* @注册地return
*/
@PostMapping("registerMapping")
public R什么equestMappingInfo registerMapping(@RequestBody Map data) {
//从请求参数中构建Requ介绍estMappingInfo对象;包公司括请求的资源路径,执行控制器类以及方法等
RequestMapp地址ingInfo mappingInfo = Request网络公司MappingInfo.paths(data.get("path").toString()).build();
//通过Spring IOC容器获取对应的bean实例
Object bean = applicationContext.getBean(data.get("bean").toString()需要);
try {
//注册Mapping信息逻辑
requestMap注册pingHandlerMapping.registerMapping(mappingInfo, bean, bean.getClass().getDeclaredMethod(data.get("method").toString(), null));
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return mappingInfo;
地址 }
/**
* 动态删除路径映射
*
* @param data
* @return
*文档/
@PostMapping("unregisterMapping")
public RequestMappingInfo unregisterMapping(@RequestBody Map data) {
//根据请求参数封装RequestMappingInfo对象,这里主要根据path路径来做删除注册逻辑
RequestMappingInfo mappingInfo = RequestMappingInfo.paths(data.get("path").什么toString()).build();
资料 //执行删除注册逻辑
requestMappingHandlerMapping.unregi需要sterMapping(mappingInfo);
retu网络公司rn mappingInfo;
}
2.编写测试用的控制器类,来作为动态注册资源路径的处理器
示例代码:https://github.co注册m/netbuffer/spring-boot-demo/blob/master/src/main/java/cn/netbuffer/springboot/demo/controller/DynamicHandler.java
@RestController
public class DynamicHandler {
public String test() {
log.debug("test execute");
return this.getClass().getSimpleName();
}
}
运行效果
1.启动工程,访问/mapping/registerMapping接口注册Mapping映射信息,来实现动态资源路径注册效果
2.注册完成后,访问我们设置的path:/yourpath,可以看到我们的控制器方法已经执行了
后台日志打印
3.再次访问删文档除注册信息接口/mapping/u内容nregisterMapping,来删除我们刚刚添加的映射信息
4.此时再访问/yourpath资源路径,发现已经返回404了~
完整测试工程参考:https://github.com/netbuffer/sp公司介绍ring-boot-demo
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至123456@qq.com 举报,一经查实,本站将立刻删除。