openFeigin添加@EnableClients报Error creating bean with name ‘requestMappingHandlerMapping‘问题分析

导读:本篇文章讲解 openFeigin添加@EnableClients报Error creating bean with name ‘requestMappingHandlerMapping‘问题分析,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

概述

在项目中引入openfeign后,在启动类上添加@EnableFeignClients()出现requestMappingHandlerMapping创建报错的问题,感觉比较奇怪为什么添加@EnableFeignClients会报错呢?

现象

main [org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext:557]-Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. 
Cannot map 'com.study.api.OrderServiceApi' method 
public abstract com.study.dto.Result<java.lang.Void> com.study.api.OrderServiceApi.createOrder(OrderServiceApi.api.dto.req.CreateOrderReq)
to {POST /api/order/createOrder}: There is already 'orederServiceController' bean method
public OrderServiceApi.api.dto.Result<java.lang.Void> com.study.orderService.controller.OrderServiceController.createOrder(com.study.api.dto.req.CreateOrderReq) mapped.

通过上面报错可以看到,在OrderServiceApi映射createOrder方法是发现已经有orederServiceController映射了createOrder方法,导致一个方法被两个handler映射。
下面我们先看看openFegin的引入

openFegin引入

         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-openfeign-core</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
			<version>2.1.0.RELEASE</version>
		</dependency>

原因分析

跟踪源码发现错误是在org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.MappingRegistry#register方法中产生的。

		public void register(T mapping, Object handler, Method method) {
			this.readWriteLock.writeLock().lock();
			try {
				HandlerMethod handlerMethod = createHandlerMethod(handler, method);
				assertUniqueMethodMapping(handlerMethod, mapping);
				this.mappingLookup.put(mapping, handlerMethod);

				List<String> directUrls = getDirectUrls(mapping);
				for (String url : directUrls) {
					this.urlLookup.add(url, mapping);
				}
               ...
             }
    
    		private void assertUniqueMethodMapping(HandlerMethod newHandlerMethod, T mapping) {
			HandlerMethod handlerMethod = this.mappingLookup.get(mapping);
			if (handlerMethod != null && !handlerMethod.equals(newHandlerMethod)) {
				throw new IllegalStateException(
						"Ambiguous mapping. Cannot map '" +	newHandlerMethod.getBean() + "' method \n" +
						newHandlerMethod + "\nto " + mapping + ": There is already '" +
						handlerMethod.getBean() + "' bean method\n" + handlerMethod + " mapped.");
			}
		}

源码中assertUniqueMethodMapping对handlerMethod做了唯一性校验。
那问题来了,为什么会有两个handler呢?
orederServiceController映射了createOrder方法,是正常的,但是OrderServiceApi却不正常了。
问题原因就在于@EnableFeignClients这个注解,通过查看源码发现这个注解如果不配置basePackages属性,就会把当前类所在的包左右basePackages,把basePackages的所有带@FeignClient的类都注册为bean。这样就会导致多个handler处理一个Url了。

总结

因此在使用@EnableFeignClients注解时,如果不调用外部的feign方法,可以不用在启动类上添加该注解,只有在调用外部feign接口时才添加该注解并把basePackages配置成外部的feign接口所在的包路径。
@EnableFeignClients是用来帮忙我们扫描外部的feign接口的,如果不掉外部的feign接口无需添加。
在不添加@EnableFeignClients这个注解的时候,项目本身使用了@FeignClient注解标注的接口是可以提供正常的feign功能的。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/99895.html

(0)
小半的头像小半

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!