解决Springboot中Feign接口和Restful接口相同Path冲突问题

Published on 2022-11-24 13:57 in 分类: 随笔 with 狂盗一枝梅
分类: 随笔

原因是SpringMVC在扫描restful接口的时候扫描了带有RequestMapping注解的类,其实我觉得大可不必。。。 2022-11-24_135556.jpg

解决方法就是修改扫描规则,如果类上已经有了FeignClient注解,就忽略掉即可。


import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

/**
 * @author kdyzm
 * @date 2022/11/14
 */
@Configuration
@ConditionalOnClass({FeignClient.class})
public class FeignConfiguration {

    @Bean
    public WebMvcRegistrations feignWebRegistrations() {
        return new WebMvcRegistrations() {
            @Override
            public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
                return new FeignRequestMappingHandlerMapping();
            }
        };
    }

    private static class FeignRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
        @Override
        protected boolean isHandler(Class<?> beanType) {
            if (AnnotatedElementUtils.hasAnnotation(beanType, FeignClient.class)) {
                return false;
            }
            return super.isHandler(beanType);
        }
    }

}


#java #feign
目录
复制 复制成功