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

这篇具有很好参考价值的文章主要介绍了springmvc 获取项目中的所有请求路径。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

1. 编写业务代码
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;


   @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属性获取文章来源地址https://www.toymoban.com/news/detail-696511.html

[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. 返回结果
{
    "success": true,
    "message": "",
    "show": false,
    "code": 200,
    "data": [
        {
            "method": "uiConfiguration",
            "className": "springfox.documentation.swagger.web.ApiResourceController",
            "type": [],
            "url": "/swagger-resources/configuration/ui"
        },
        {
            "method": "swaggerResources",
            "className": "springfox.documentation.swagger.web.ApiResourceController",
            "type": [],
            "url": "/swagger-resources"
        },
        {
            "method": "securityConfiguration",
            "className": "springfox.documentation.swagger.web.ApiResourceController",
            "type": [],
            "url": "/swagger-resources/configuration/security"
        },
        {
            "method": "error",
            "className": "org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController",
            "type": [],
            "url": "/error"
        },
        {
            "method": "errorHtml",
            "className": "org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController",
            "type": [],
            "url": "/error"
        }
    ],
    "result": null
}

到了这里,关于springmvc 获取项目中的所有请求路径的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Java 多种获取项目路径下的文件

    目标文件放在项目的resources文件夹下 的 mytxt文件里面,文件名叫 file Test.txt: 其实可以看到,项目运行后,这个文件被丢到了target文件夹下: 拿到这个文件的 InputStream : 比如我们在FileUtil里面写个获取文件流的方法, public class FileUtil {      }   ① getResourceAsStream  String

    2024年02月04日
    浏览(42)
  • Postman+Java springboot演示 get post put delete请求并携带(路径 路径问号后 json 表单)参数形式

    我们先创建一个java的springboot工程 在项目中 找到启动类的位置目录 在项目创建一个类 叫 user 我是想将 user 当做一个属性类的 按规范来讲 我们可以创建一个entity包 然后在下面去创建属性类 但这里 我们不想搞那么麻烦了 毕竟只是练习一下 然后 user参考代码如下 这里 只是定

    2024年02月06日
    浏览(50)
  • 【Java项目】解决请求路径上明文ID传输导致可能被攻击的方法

    这个问题是我公司的一个小业务问题,问题来源于我们发送请求的时候,请求路径上携带的是明文,比如http://xxx/xxx/id=12345,那么别有用心的人就可能会推测出id的生成策略,导致遍历id,自增id的方式来获取我们服务的数据,这是很危险的,所以我的mentor就叫我想一个解决方

    2024年02月12日
    浏览(198)
  • 34.SpringMVC获取请求参数

    将 HttpServletRequest 作为 控制器方法的形参 ,此时HttpServletRequest类型的参数表示 封装了当前请求 的请求报文的对象 index.html TestParamController.java 成功获取到表单提交的信息,这是采用原生Servlet的方式获取 在控制器方法的形参位置, 设置和请求参数同名的形参 ,当浏览器发送

    2024年02月12日
    浏览(41)
  • SpringMVC(三)获取请求参数

    SpringMVC封装的就是原生的servlet 我们进行测试如下所示: 我们的登陆页面如下所示:  点击登录之后,即会跳转到成功界面。 同时我们的控制台会进行输出:username:admin,password:123456 我们的页面如下所示: 我们进行如下界面: 我们点击登陆之后,会跳转到成功界面, 此时控

    2024年02月08日
    浏览(37)
  • SpringMVC-获取请求参数

    用户输入信息后,如果想要得到用户输入的内容 , springMVC 应该如何做呢? 本次课讲解下再springmvc中获取请求参数及中文乱码问题 通过servletAPI获取 讲HttpServletRequest作为控制器方法的形参,此时HttpServletRequest类型的参数表示封装了当前请求的请求报文的对象 通过控制器方法的形

    2024年01月21日
    浏览(47)
  • SpringMVC获取请求参数

    将HttpServletRequest作为控制器方法的形参,此时HttpServletRequest类型的参数表示封装了当前请求的请求报文的对象 在控制器方法的形参位置,设置和请求参数同名的形参,当浏览器发送请求,匹配到请求映射时,在DispatcherServlet中就会将请求参数赋值给相应的形参 注: 若请求所

    2024年02月08日
    浏览(39)
  • SpringMVC之获取请求参数

    下面用到了thymeleaf,不知道的可以看我同专栏里的搭建框架这篇文章。 将HttpServletRequest作为控制器方法的形参,此时HttpServletRequest类型的参数表示封装了当前请求的请求报文的对象。 在控制器方法的形参位置,设置和请求参数同名的形参,当浏览器发送请求,匹配到请求映

    2024年02月12日
    浏览(44)
  • springMVC获取请求参数的方式

    将HttpServletRequest作为控制器方法的形参,此时HttpServletRequest类型的参数表示封装当前请求的请求报文 只需要在控制器方法的形参位置,设置一个形参,形参的名字和请求参数的名字一致即可 后端根据前端页面传入的实际参数进行一一取值(参数的少的情况下建议使用) @Re

    2024年02月02日
    浏览(51)
  • 详解 SpringMVC 中获取请求参数

    在Spring MVC中,获取请求参数有很多种方式,先来个开盘小菜,使用@RequestParam注解来获取请求参数。 @RequestParam注解可以用于方法的参数上,用于指定请求参数的名称。例如,如果你的请求参数名为\\\"username\\\",你可以在方法参数上使用@RequestParam(“username”)来获取该参数的值。

    2024年02月10日
    浏览(40)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包