重写DispatcherServlet获取springmvc 所有RequestMapping的url
程序员文章站
2022-03-07 10:02:24
...
1、重写DispatcherServlet
import java.util.HashSet; import java.util.Map; import java.util.Set; import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.context.ApplicationContext; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition; import org.springframework.web.servlet.mvc.method.RequestMappingInfo; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; @SuppressWarnings("serial") public class MyDispatcherServlet extends DispatcherServlet { public static final Set<String> MAPPING_URLS = new HashSet<>(); @Override protected void initStrategies(ApplicationContext context) { super.initStrategies(context); Map<String, HandlerMapping> matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false); for(HandlerMapping handlerMapping : matchingBeans.values()) { if(handlerMapping instanceof RequestMappingHandlerMapping) { RequestMappingHandlerMapping rmhm = (RequestMappingHandlerMapping) handlerMapping; Map<RequestMappingInfo, HandlerMethod> handlerMethods = rmhm.getHandlerMethods(); for(RequestMappingInfo rmi : handlerMethods.keySet()) { PatternsRequestCondition prc = rmi.getPatternsCondition(); MAPPING_URLS.addAll(prc.getPatterns()); } } } System.out.println(">>>>>>>>>>>>>>>>"); for (String string : MAPPING_URLS) { System.out.println(string); } System.out.println(">>>>>>>>>>>>>>>>"); } }
2、在web.xml中配置重写的MyDispatcherServlet
<!-- MVC Servlet --> <servlet> <servlet-name>springServlet</servlet-name> <servlet-class>com.huatech.web.listener.MyDispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springServlet</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping>
上一篇: java常用命令
下一篇: poi excel导入工具类