你的位置:首页 > 软件开发 > ASP.net > spring mvc 获取所有的controller和url映射关系

spring mvc 获取所有的controller和url映射关系

发布时间:2015-09-07 12:00:07
有时候需要根据url反查controller,如果能获取所有的url,则不用跟据url去代码里搜了,方便开发人员、调试人员或交接人。关键对象:RequestMappingHandlerMapping Java代码 import java.util.ArrayList ...

有时候需要根据url反查controller,如果能获取所有的url,则不用跟据url去代码里搜了,方便开发人员、调试人员或交接人。

关键对象:RequestMappingHandlerMapping 

Java代码 spring mvc 获取所有的controller和url映射关系 spring mvc 获取所有的controller和url映射关系spring mvc 获取所有的controller和url映射关系
  1. import java.util.ArrayList;  
  2. import java.util.HashMap;  
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6. import org.springframework.beans.factory.annotation.Autowired;  
  7. import org.springframework.stereotype.Controller;  
  8. import org.springframework.ui.Model;  
  9. import org.springframework.web.bind.annotation.RequestMapping;  
  10. import org.springframework.web.method.HandlerMethod;  
  11. import org.springframework.web.servlet.get='_blank'>mvc.condition.PatternsRequestCondition;  
  12. import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;  
  13. import org.springframework.web.servlet.mvc.method.RequestMappingInfo;  
  14. import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;  
  15.   
  16. @Controller  
  17. public class MappingController {  
  18.   
  19.     @Autowired  
  20.     private RequestMappingHandlerMapping requestMappingHandlerMapping;  
  21.   
  22.     @RequestMapping(value = "/mappings")  
  23.     public String list(Model model) {  
  24.         List<HashMap<String, String>> urlList = new ArrayList<HashMap<String, String>>();  
  25.   
  26.         Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();  
  27.         for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {  
  28.             HashMap<String, String> hashMap = new HashMap<String, String>();  
  29.             RequestMappingInfo info = m.getKey();  
  30.             HandlerMethod method = m.getValue();  
  31.             PatternsRequestCondition p = info.getPatternsCondition();  
  32.             for (String url : p.getPatterns()) {  
  33.                 hashMap.put("url", url);  
  34.             }  
  35.             hashMap.put("className", method.getMethod().getDeclaringClass().getName()); // 类名  
  36.             hashMap.put("method", method.getMethod().getName()); // 方法名  
  37.             RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();  
  38.             String type = methodsCondition.toString();  
  39.             if (type != null && type.startsWith("[") && type.endsWith("]")) {  
  40.                 type = type.substring(1, type.length() - 1);  
  41.                 hashMap.put("type", type); // 方法名  
  42.             }  
  43.             urlList.add(hashMap);  
  44.         }  
  45.         model.addAttribute("list", urlList);  
  46.         return "/console/system/mappingList";  
  47.     }  
  48.   
  49. }  

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:spring mvc 获取所有的controller和url映射关系

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录