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

一枝梅 2月前 ⋅ 161 阅读

原因是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
我有话说:

全部评论: 0

    文章目录