@EnableFeignClients和@FeignClient在自动装配'FeignContext'NoSuchBeanException时失败 [英] @EnableFeignClients and @FeignClient fail on Autowiring 'FeignContext' NoSuchBeanException

查看:508
本文介绍了@EnableFeignClients和@FeignClient在自动装配'FeignContext'NoSuchBeanException时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写的微服务需要与我们平台中的其他微服务进行通信。在这次尝试中,我们的理想解决方案是 Spring Cloud Netflix Feign ,实现 @FeignClient

The microservice I'm writting needs to communicate to other microservices in our platform. On that attempt, the ideal solution for us is Spring Cloud Netflix Feign, implemeting a @FeignClient.

但是,当我尝试 @Autowired ReviewProvider 时,我面临以下例外:

However, I'm facing the exception below when I try an @Autowired ReviewProvider:

例外(原因)

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.cloud.netflix.feign.FeignContext' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:351)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093)
    at org.springframework.cloud.netflix.feign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:155)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)

ReviewProvider.java

@FeignClient("http://metadata-reviews")
public interface ReviewProvider {

    @RequestMapping(path = "sessions", method = POST)
    ReviewSessionDTO createSession();

}

ReviewProvider.java

@RunWith(SpringRunner.class)
@ActiveProfiles(INTEGRATION)
@ContextConfiguration(classes = AppEntry.class)
@AutoConfigureTestDatabase(replace = Replace.NONE)
@DataJpaTest
public class ReviewProviderTest {

    @Autowired
    private ReviewProvider provider;
    private Class<? extends ReviewProvider> providerClass;

    @Before
    public void setup() {
        providerClass = provider.getClass();
    }

    @Test
    public void classAnnotations() {
        assertTrue(providerClass.isAnnotationPresent(FeignClient.class));
        assertEquals("http://metadata-reviews", providerClass.getAnnotation(FeignClient.class).value());
    }

    @Test
    public void createSession() throws Exception {
        final Method method = providerClass.getDeclaredMethod("createSession");
        assertTrue(method.isAnnotationPresent(RequestMapping.class));

        final RequestMapping mapping = method.getAnnotation(RequestMapping.class);
        assertEquals("sessions", mapping.path());
        assertEquals(0, method.getParameters().toString());
    }
}


推荐答案

似乎就像那里没有关于这个东西的解决方案...

Seems like there is not anything out there yet about the solution to this stuff...

这是我为解决这个问题所做的:
1.添加此注释到你的测试类:

Here is what I did to solve this: 1. Add this annotation to your test class:

@ImportAutoConfiguration({RibbonAutoConfiguration.class, FeignRibbonClientAutoConfiguration.class, FeignAutoConfiguration.class})

试试吧,如果它不起作用,你可能需要 @EnableFeignClients 注释

Try it, if it does not work, you might need the @EnableFeignClients annotation on your main program config

这篇关于@EnableFeignClients和@FeignClient在自动装配'FeignContext'NoSuchBeanException时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆