↓ 跳过正文

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

正能量 1 分钟阅读 更新于 2026-10-18

说明通过 RequestMappingHandlerMapping 查看 Spring MVC 映射的方法与使用范围。

Java 编程:原站配图。
@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/>");
        }
    }

补充阅读
#

说明通过 RequestMappingHandlerMapping 查看 Spring MVC 映射的方法与使用范围。

如何理解与使用
#

示例读取处理器维护的映射信息,可用于理解控制器方法怎样对应到请求路径;它不是一次真实 HTTP 调用的执行轨迹。

验证与边界
#

调试输出应结合 HTTP 方法、路径条件和实际可达性查看,并限制此类诊断接口的访问范围,避免公开内部管理端点清单。