获取SpringMVC中所有RequestMapping映射URL信息


SpringMVC启动的时候,会把接口信息收集在RequestMappingHandlerMapping中,故可以通过这个类,拿到全部的映射信息,Sample代码段如下:

@Autowired
private ApplicationContext applicationContext;



Set<String> noLoginUrlSet = new HashSet<>();
RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
Map<RequestMappingInfo, HandlerMethod> handlerMethods = mapping.getHandlerMethods();// 就是这个
for (RequestMappingInfo rmi : handlerMethods.keySet()) {
   HandlerMethod handlerMethod = handlerMethods.get(rmi);
   if (handlerMethod.hasMethodAnnotation(NoLogin.class)) {
      PatternsRequestCondition prc = rmi.getPatternsCondition();
      Set<String> patterns = prc.getPatterns();
      noLoginUrlSet.addAll(patterns);
   }
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM