java @EnableFeignClients 和 @FeignClient 在自动装配 'FeignContext' NoSuchBeanException 时失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43093968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@EnableFeignClients and @FeignClient fail on Autowiring 'FeignContext' NoSuchBeanException
提问by hudsonmendes
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
.
我正在编写的微服务需要与我们平台中的其他微服务进行通信。在那次尝试中,对我们来说理想的解决方案是Spring Cloud Netflix Feign,它实现了一个@FeignClient
.
However, I'm facing the exception below when I try an @Autowired ReviewProvider
:
但是,当我尝试以下时,我遇到了以下异常@Autowired ReviewProvider
:
Exception (cause)
异常(原因)
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
评论提供者.java
@FeignClient("http://metadata-reviews")
public interface ReviewProvider {
@RequestMapping(path = "sessions", method = POST)
ReviewSessionDTO createSession();
}
ReviewProvider.java
评论提供者.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());
}
}
回答by Codetector
Seems like there is not anything out there yet about the solution to this stuff...
似乎还没有关于解决这个问题的任何东西......
Here is what I did to solve this: 1. Add this annotation to your test class:
这是我为解决这个问题所做的: 1. 将此注释添加到您的测试类中:
@ImportAutoConfiguration({RibbonAutoConfiguration.class, FeignRibbonClientAutoConfiguration.class, FeignAutoConfiguration.class})
Try it, if it does not work, you might need the @EnableFeignClients
annotation on your main program config
试试吧,如果不行,你可能需要@EnableFeignClients
主程序配置上的注解
回答by MariuszS
Recommended approach is to slice application configuration, this means you need to remove @EnableFeignClients
from SpringBootApplication.
推荐的方法是对应用程序配置进行切片,这意味着您需要@EnableFeignClients
从SpringBootApplication 中删除 。
and add dedicated configuration class:
并添加专用配置类:
@Configuration
@EnableFeignClients
public class CloudConfiguration {
}
This is required, because all slices annotation (like @WebMvcTest) include default configuration from SpringBootApplication.
这是必需的,因为所有切片注释(如@WebMvcTest)都包含来自SpringBootApplication 的默认配置。
Reference:
参考:
回答by Ivan Osipov
The only what you must to do:
您唯一必须做的:
- add to your build file feign dependency, for example, for gradle
- 添加到您的构建文件 feign 依赖项,例如,对于 gradle
compile 'org.springframework.cloud:spring-cloud-starter-feign'
编译'org.springframework.cloud:spring-cloud-starter-feign'
- add @FeignClient to your interface
- add @EnableFeignClients to any configuration or to class with annotation @SpringBootApplication
- 将@FeignClient 添加到您的界面
- 将 @EnableFeignClients 添加到任何配置或带有注释 @SpringBootApplication 的类
回答by adramazany
I solved this problem just by @EnableAutoConfiguration annotation on my class
我只是通过我的班级上的 @EnableAutoConfiguration 注释解决了这个问题