Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@EnableFeignClients and @FeignClient fail on Autowiring 'FeignContext' NoSuchBeanException

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.

However, I'm facing the exception below when I try an @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

@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());
    }
}
like image 546
hudsonmendes Avatar asked Mar 29 '17 12:03

hudsonmendes


People also ask

What is EnableFeignClients?

FeignClient Basics. The Feign client uses a declarative approach for accessing the API. To use it, we must first enable the Spring Cloud support for it on our Spring Boot Application with the @EnableFeignClients annotation at the class level on a @Configuration class.

What is spring OpenFeign?

In this tutorial, we're going to describe Spring Cloud OpenFeign — a declarative REST client for Spring Boot apps. Feign makes writing web service clients easier with pluggable annotation support, which includes Feign annotations and JAX-RS annotations.

What is name in feign client?

A central concept in Spring Cloud's Feign support is that of the named client. Each feign client is part of an ensemble of components that work together to contact a remote server on demand, and the ensemble has a name that you give it as an application developer using the @FeignClient annotation.


3 Answers

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:

    @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

like image 172
Codetector Avatar answered Nov 05 '22 22:11

Codetector


Recommended approach is to slice application configuration, this means you need to remove @EnableFeignClients from 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.

Reference:

  • https://github.com/spring-projects/spring-boot/issues/7270
  • https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#test-auto-configuration
like image 25
MariuszS Avatar answered Nov 05 '22 20:11

MariuszS


The only what you must to do:

  • add to your build file feign dependency, for example, for gradle

compile 'org.springframework.cloud:spring-cloud-starter-feign'

  • add @FeignClient to your interface
  • add @EnableFeignClients to any configuration or to class with annotation @SpringBootApplication
like image 24
Ivan Osipov Avatar answered Nov 05 '22 20:11

Ivan Osipov