首页 > 其他分享 >pringboot/springmvc 获取项目中的所有请求路径

pringboot/springmvc 获取项目中的所有请求路径

时间:2023-09-06 22:34:14浏览次数:38  
标签:web pringboot java springmvc 路径 springframework doFilter org ApplicationFilterCh

springboot/springmvc 获取项目中的所有请求路径

1. 编写业务代码


   @Autowired
	private WebApplicationContext applicationContext;

   @GetMapping("/getAllURL")
	public RestfulResult getAllURL() {
		// 获取springmvc处理器映射器组件对象 RequestMappingHandlerMapping无法直接注入
		RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
		//获取url与类和方法的对应信息
		Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();

		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		for (Map.Entry<RequestMappingInfo, HandlerMethod> handlerMethodEntry : map.entrySet()) {
			Map<String, Object> urlInfoMap = new HashMap<String, Object>();
			RequestMappingInfo info = handlerMethodEntry.getKey();
			HandlerMethod method = handlerMethodEntry.getValue();
			// 获取url
			PatternsRequestCondition patternsCondition = info.getPatternsCondition();
			for (String url : patternsCondition.getPatterns()) {
				urlInfoMap.put("url", url);
			}
			// 反射获取url对应类名和方法名
			// 类名
			urlInfoMap.put("className", method.getMethod().getDeclaringClass().getName());
			// 方法名
			urlInfoMap.put("method", method.getMethod().getName());

			// 获取请求类型
			RequestMethodsRequestCondition methodsRequestCondition = info.getMethodsCondition();
			List<String> requestMethodList = new ArrayList<String>(methodsRequestCondition.getMethods().size());
			for (RequestMethod requestMethod : methodsRequestCondition.getMethods()) {
				requestMethodList.add(requestMethod.toString());
			}
			urlInfoMap.put("type", requestMethodList);
			result.add(urlInfoMap);
		}
		return  RestfulResult.getSuccessResult(result);
	}

2. 异常信息

No qualifying bean of type 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping' available: expected single matching bean but found 3: swagger2ControllerMapping,requestMappingHandlerMapping,controllerEndpointHandlerMapping

原因就是: 基于 RequestMappingHandlerMapping.class 找到了三个 ,故加 上name属性获取

[ERROR] 21:20:41.409 [http-nio-8080-exec-2] o.a.c.c.C.[.[.[.[dispatcherServlet] 175 Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping' available: expected single matching bean but found 3: swagger2ControllerMapping,requestMappingHandlerMapping,controllerEndpointHandlerMapping] with root cause
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping' available: expected single matching bean but found 3: swagger2ControllerMapping,requestMappingHandlerMapping,controllerEndpointHandlerMapping
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1200)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveBean(DefaultListableBeanFactory.java:420)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:349)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:342)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1127)
	at controller.EnumController.getAllURL(EnumController.java:40)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:645)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:750)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:103)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at com.redxun.datasource.DataSourceFilter.doFilter(DataSourceFilter.java:13)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:124)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at com.redxun.common.filter.MessageFilter.doFilter(MessageFilter.java:19)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at com.redxun.common.filter.TraceFilter.doFilter(TraceFilter.java:29)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at com.redxun.common.filter.WebContextFilter.doFilter(WebContextFilter.java:27)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at com.redxun.web.filter.WebContextFilter.doFilter(WebContextFilter.java:30)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)
	at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126)
	at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:118)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:158)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.header.HeaderWriterFilter.doHeadersAfter(HeaderWriterFilter.java:92)
	at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:77)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
	at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
	at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
	at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:97)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1707)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.base/java.lang.Thread.run(Thread.java:834)

3. 调整后的代码


	
    @Autowired
	private WebApplicationContext applicationContext;

	
    
    @GetMapping("/getAllURL")
	public RestfulResult getAllURL() {
		// 获取springmvc处理器映射器组件对象 RequestMappingHandlerMapping无法直接注入
		Map<RequestMappingInfo, HandlerMethod> mapping = applicationContext.getBean("requestMappingHandlerMapping", RequestMappingHandlerMapping.class).getHandlerMethods();
		//获取url与类和方法的对应信息

		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		for (Map.Entry<RequestMappingInfo, HandlerMethod> handlerMethodEntry : mapping.entrySet()) {
			Map<String, Object> urlInfoMap = new HashMap<String, Object>();
			RequestMappingInfo info = handlerMethodEntry.getKey();
			HandlerMethod method = handlerMethodEntry.getValue();
			// 获取url
			PatternsRequestCondition patternsCondition = info.getPatternsCondition();
			for (String url : patternsCondition.getPatterns()) {
				urlInfoMap.put("url", url);
			}
			// 反射获取url对应类名和方法名
			// 类名
			urlInfoMap.put("className", method.getMethod().getDeclaringClass().getName());
			// 方法名
			urlInfoMap.put("method", method.getMethod().getName());

			// 获取请求类型
			RequestMethodsRequestCondition methodsRequestCondition = info.getMethodsCondition();
			List<String> requestMethodList = new ArrayList<String>(methodsRequestCondition.getMethods().size());
			for (RequestMethod requestMethod : methodsRequestCondition.getMethods()) {
				requestMethodList.add(requestMethod.toString());
			}
			urlInfoMap.put("type", requestMethodList);
			result.add(urlInfoMap);
		}
		return  RestfulResult.getSuccessResult(result);
	}
 

4. 返回结果


标签:web,pringboot,java,springmvc,路径,springframework,doFilter,org,ApplicationFilterCh
From: https://blog.51cto.com/u_4981212/7390957

相关文章

  • springboot vue人事管理系统源码
    开发环境及工具:大等于jdk1.8,大于mysql5.5,idea(eclipse),nodejs,vscode(webstorm)技术说明:springbootmybatisvueelementui代码注释齐全,没有多余代码,适合学习(毕设),二次开发,包含论文技术相关文档。功能介绍:员工端:考勤打卡:定位当前位置,打卡需要在打卡范围内才可打卡(后台设置打卡范围)我的......
  • 百度SpringMVC上传下载组件
    ​ 之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需求,都能得到满足。小小开心了一把。  但无论插件再怎么灵活,也难以应付所有的需求,比......
  • 百度SpringBoot上传下载组件
    ​ 这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数下面直接贴代码吧,一些难懂的我大部分都加上注释了:上传文件实体类:看得出来,实体类中已经有很多我们需要的功能了,还有实用的属性。如MD5秒传的信息。 ......
  • Maven 安装与环境变量配置 及 springboot项目创建
    (1)Maven安装:在官网下载:http://maven.apache.org/download.cgi  (2)Maven配置环境变量:解压到一个路径,然后配置环境变量:新建变量名:MAVEN_HOME变量值:D:\apache-maven-3.6.0(这是我的MAVEN路径)编辑变量名:Path在最前面加上:%MAVEN_HOME%\bin新建系统变量--》......
  • springboot下载文件 范围下载
    springboot下载文件范围下载关键词:springboot,download,Range,Content-Range,Content-Length,httpcode206PartialContent下载文件的一部分,我们在requestheader:Range中指定要获取的文件的字节范围。要注意httpresponseheader:Content-Length一定要与Range中所表示的获取bytes......
  • SpringBoot3.x 框架搭建
    SpringBoot3.0.0已经发布一段时间了,越来越多的公司考虑将技术框架升级到最新版本,JDK也相应要求JDK17以上。对应SpringBoot2.x的版本,建议先升级到SpringBoot2.7.0,之后再升级到SpringBoot3.x。 gitee源码地址:https://gitee.com/yhc910/spring-boot-framework.git  ......
  • C# 获取程序路径的几种方法及其区别
    在开发的程序中使用自定义的配置文件属于基本的操作了,但最近发现一个问题:如果程序是由用户通过exe文件直接打开的,获取同目录下配置文件不会有问题;但如果程序是由第三方应用启动的,通过不同方式获取的“当前目录”会有不同。以下是整理的几种获取当前目录的方式,及官方文档中的注释:......
  • SpringBoot启动报数组下标越界
    问题描述:启动读取配置文件时报错关键字:ERRORorg.springframework.boot.SpringApplication-Applicationrunfailedjava.lang.ArrayIndexOutOfBoundsException:-1ConnectedtothetargetVM,address:'127.0.0.1:58753',transport:'socket'2023-09-0611:09......
  • SpringBoot整合Redis
    SpringBoot整合Redis整合springcache导入依赖<!--引入redis--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>......
  • 007-SpringBoot+Mybatis+Sqlite框架搭建
    1,配置文件(application.yaml)server:port:6695spring:datasource:url:jdbc:sqlite:D:/examtolearn.dbusername:password:driver-class-name:org.sqlite.JDBCmybatis:mapper-locations:classpath:mapper/*.xmlconfiguration:log-......