@EnableFeignClients and @FeignClient not working when Autowiring 'FeignContext' NoSuchBeanException
The microservice I'm writing needs to communicate with other microservices on our platform. In this attempt, the ideal solution for us is the Spring Cloud Netflix Feign by implementing . @FeignClient
However, when I try to execute @Autowired ReviewProvider
:
Exception (reason)
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());
}
}
It seems that nothing has been found yet about solving this question ...
Here's what I did to solve this problem: 1. Add this annotation to the test class:
@ImportAutoConfiguration({RibbonAutoConfiguration.class, FeignRibbonClientAutoConfiguration.class, FeignAutoConfiguration.class})
Try it, if that doesn't work, you may need an annotation @EnableFeignClients
in your main program config
The recommended approach is to set up a slice application , which means you need to remove @EnableFeignClients
from SpringBootApplication.
and add a dedicated config class:
@Configuration
@EnableFeignClients
public class CloudConfiguration {
}
This is required because all fragment annotations (like @WebMvcTest) include the default configuration from SpringBootApplication.
Link:
- https://github.com/spring-projects/spring-boot/issues/7270
- https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#test-auto-configuration
The only thing you need to do is:
- add to your file dependency build file like for gradle
compile 'org.springframework.cloud: spring-cloud-starter-feign'
- add @FeignClient to your interface
- add @EnableFeignClients to any config or class annotated with @SpringBootApplication