Is it possible to provide my own implementation of a @FeignClient annotated interface based on a configuration setting? I'd like to be able to define a "local" Spring profile, for example, that will use a mocked out interface that will not try to connect to any external endpoints, allowing me to run my Spring boot application without any external dependencies being up. Additional profiles would allow the clients to be instantiated and injected as they currently are.
I have tried using the @ConditionalOnProperty annotation to serve up a bean for the interface as follows, but that doesn't work:
@FeignClient(name = "http://foo-service")
public interface FooResource {
@RequestMapping(value = "/doSomething", method = GET)
String getResponse();
}
...
@Configuration
public class AppConfig {
@Bean
@ConditionalOnProperty(prefix = "spring.profile", name = "active", havingValue="local")
public FooResource fooResource() {
return new FooResource() {
@Override
public String getResponse() {
return "testing";
}
};
}
}
When the application launches the bean is ignored, and my FooResource instance still tries to contact the remote service.
How can I achieve my aim?
Comment From: ryanjbaxter
This might be something Spring Cloud Contract can help with. Although generally the contracts are only used in tests as far as I know. @marcingrzejszczak thoughts?
Comment From: spencergibb
Why not use the @Profile annotation? Put @EnableFeignClients in a @Profile("!local").
Comment From: marcingrzejszczak
Spring Cloud Contract mocks out the service discovery in such a way that your call will get redirected to a fake HTTP server stub (e.g. WireMock)
Comment From: gala-dan
Thanks @spencergibb - you're suggestion worked out well. Appreciate the quick response.
Comment From: mattmadhavan
Hello @spencergibb and @gala-dan , Helped me too!
Thanks Matt