Java Spring Boot 获得所有的url

Java Spring Boot 获得所有的url

@Controller
@Slf4j
@RequestMapping("console")
public class ControllerConsole {
    @Autowired
    @Qualifier("jsonView")
    private View jsonView;
    
    @Autowired
    WebApplicationContext applicationContext;
    
    @RequestMapping(value = "getAllUrl", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ModelAndView getAllUrl(HttpServletRequest request, HttpServletResponse response){
        ModelAndView modelAndView = new ModelAndView(jsonView);

        RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        //获取url与类和方法的对应信息
        Map<RequestMappingInfo,HandlerMethod> map = mapping.getHandlerMethods();
        List<String> urlList = new ArrayList<>();
        for (RequestMappingInfo info : map.keySet()){
            //获取url的Set集合,一个方法可能对应多个url
            Set<String> patterns = info.getPatternsCondition().getPatterns();
            for (String url : patterns){
                urlList.add(url);
            }
            modelAndView.addObject("urlArray", urlList);
            modelAndView.addObject("urlArraySize", urlList.size());
        }
        return modelAndView;
    }

    @RequestMapping(value = "getAllUrlDetail", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ModelAndView getAllUrlDetail(HttpServletRequest request, HttpServletResponse response){
        ModelAndView modelAndView = new ModelAndView(jsonView);

        RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        // 获取url与类和方法的对应信息
        Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();

        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
            Map<String, String> map1 = new HashMap<String, String>();
            RequestMappingInfo info = m.getKey();
            HandlerMethod method = m.getValue();
            PatternsRequestCondition p = info.getPatternsCondition();
            for (String url : p.getPatterns()) {
                map1.put("url", url);
            }
            map1.put("className", method.getMethod().getDeclaringClass().getName()); // 类名
            map1.put("method", method.getMethod().getName()); // 方法名
            RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
            for (RequestMethod requestMethod : methodsCondition.getMethods()) {
                map1.put("type", requestMethod.toString());
            }

            list.add(map1);
        }
        modelAndView.addObject("size",list.size());
        modelAndView.addObject("list",list);

        return modelAndView;
    }
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
}

结果如下:

Java Spring Boot 获得所有的url_第1张图片

这个只截取部分吧!!!
Java Spring Boot 获得所有的url_第2张图片

参考: https://blog.csdn.net/tt____tt/article/details/82012999

你可能感兴趣的:(Java Spring Boot 获得所有的url)