当前位置:正能量 » 代码编程 » Java » Spring MVC 找出所有的URL映射地址

Spring MVC 找出所有的URL映射地址

@Autowired
private RequestMappingHandlerMapping handlerMapping;
@RequestMapping("urls")
public void index(Long id, HttpServletRequest request, HttpServletResponse response)
            throws IOException, ClassNotFoundException {
        Map<RequestMappingInfo, HandlerMethod> map = handlerMapping.getHandlerMethods();
        Iterator<Entry<RequestMappingInfo, HandlerMethod>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry<RequestMappingInfo, HandlerMethod> entry = iterator.next();
            HandlerMethod hm=entry.getValue();

            StringBuffer str=new StringBuffer("请求地址:"+entry.getKey().getPatternsCondition() + "----调用方法:" + hm.getMethod().getName()+"(");
            MethodParameter[] mmm=hm.getMethodParameters();
            for (MethodParameter methodParameter : mmm) {
                str.append(methodParameter.getParameterType().getSimpleName()+",");
            }
            if(str.lastIndexOf(",")>0){
                str.deleteCharAt(str.length()-1);
            }
            str.append(")");
            System.out.println(str);
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html");
            response.getWriter().write(str+"<br/>");
        }
    }
未经允许不得转载:正能量 » Spring MVC 找出所有的URL映射地址

相关文章

评论 (0)

1 + 5 =