Skip to content

Instantly share code, notes, and snippets.

@qsLI
Created May 5, 2018 10:48
Show Gist options
  • Save qsLI/82bcea4d0e9a633a11607f9ca5285164 to your computer and use it in GitHub Desktop.
Save qsLI/82bcea4d0e9a633a11607f9ca5285164 to your computer and use it in GitHub Desktop.
print all urls in spring mvc
<%@ page import="org.springframework.context.ApplicationContext" %>
<%@ page import="org.springframework.util.MultiValueMap" %>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ page import="org.springframework.web.method.HandlerMethod" %>
<%@ page import="org.springframework.web.servlet.handler.AbstractHandlerMethodMapping" %>
<%@ page import="org.springframework.web.servlet.mvc.method.RequestMappingInfo" %>
<%@ page import="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" %>
<%@ page import="java.lang.reflect.Field" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.Set" %>
<%--
~ /* * Copyright (c) 2018 Qunar.com. All Rights Reserved. */
--%>
<%--
Created by IntelliJ IDEA.
User: qishengli
Date: 18-5-5
Time: 下午4:38
To change this template use File | Settings | File Templates.
--%>
<%
final Field mappingRegistry = AbstractHandlerMethodMapping.class.getDeclaredField("mappingRegistry");
mappingRegistry.setAccessible(true);
final Class<?> mappingRegistryInnerKlass = Class.forName("org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry");
final Field urlLookup = mappingRegistryInnerKlass.getDeclaredField("urlLookup");
final Field mappingLookup = mappingRegistryInnerKlass.getDeclaredField("mappingLookup");
urlLookup.setAccessible(true);
mappingLookup.setAccessible(true);
// mapping
ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
final AbstractHandlerMethodMapping<RequestMappingInfo> mapping = context.getBean(RequestMappingHandlerMapping.class);
final Object registry = mappingRegistry.get(mapping);
final MultiValueMap<String, RequestMappingInfo> multiValueMap = (MultiValueMap<String, RequestMappingInfo>) urlLookup.get(registry);
final Map<RequestMappingInfo, HandlerMethod> requestMappingInfoHandlerMethodMap = (Map<RequestMappingInfo, HandlerMethod>) mappingLookup.get(registry);
// output
final Set<Map.Entry<String, List<RequestMappingInfo>>> entries = multiValueMap.entrySet();
for(Map.Entry<String, List<RequestMappingInfo>> entry : entries) {
final String path = entry.getKey();
out.write(path + "<BR><ul>");
for (RequestMappingInfo mappingInfo : entry.getValue()) {
final HandlerMethod handlerMethod = requestMappingInfoHandlerMethodMap.get(mappingInfo);
out.write("<li>" + handlerMethod.getMethod().toString() + "</li><BR>");
}
out.write("</ul><BR>");
}
%>
@qsLI
Copy link
Author

qsLI commented May 5, 2018

/async
public java.util.concurrent.Callable com.air.mvc.AsyncController.asyncProcess()

/asyncV2
public org.springframework.web.context.request.async.DeferredResult com.air.mvc.AsyncController.aysncProcess2()

/mvc/echo
private java.lang.String com.air.mvc.SampleController.echo(javax.servlet.http.HttpServletRequest)

/mvc/test
private java.lang.String com.air.mvc.SampleController.echo(java.lang.Integer)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment