spring mvc 获取所有注册的url

 背景:坑货同事写代码一点规范都没有,瞎写,根据url没法直接定位到方法,无奈产生了此接口,程序员何苦为难程序员呢

    @Autowired
    private RequestMappingHandlerMapping handlerMapping;

    @GetMapping("getUrlMap")
    @ApiOperation(value = "获取所有url映射", notes = "")
    public Object getUrlMap() {
        Map<String, String> retMap = new HashMap<>();
        Map<RequestMappingInfo, HandlerMethod> map = handlerMapping.getHandlerMethods();
        Iterator<Entry<RequestMappingInfo, HandlerMethod>> iter = map.entrySet().iterator();
        Map.Entry<RequestMappingInfo, HandlerMethod> entry;
        while (iter.hasNext()) {
            entry = iter.next();
            if (entry.getValue().getBeanType().getName().contains("com.xf.controller")) {
                String url = entry.getKey().getMethodsCondition() + "  " + entry.getKey().getPatternsCondition();
                String method = entry.getValue().getMethod().toString();
                retMap.put(url, method);
            }
        }
        return JsonRet.successData(retMap);
    }
原文地址:https://www.cnblogs.com/wujf/p/10796348.html