0

How to have 2 annotation toggled for different profile for the same Feign client Interface?

I use a Feign Client Interface which has the following code when used with load balancer url. This I call as Non-Eureka for reference:

@FeignClient(name = "DEPOSIT-FEIGN-CLIENT", url = "${DEPOSIT-DATA-URL}")
public interface DepositFeignClient {

    @RequestMapping(method = RequestMethod.GET, value = "/path/to/api/{accountNumber}", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    DepositResponse getDepositDetails(@PathVariable(value = "accountNumber") String accountNumber);

}

On the other hand I use the following code when using Eureka and Spring Cloud Gateway:

@FeignClient(value = "ABCD-GATEWAY", path = "${DEPOSIT-EUREKA-APPNAME}")
public interface DepositFeignClient {

    @RequestMapping(method = RequestMethod.GET, value = "/path/to/api/{accountNumber}", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    DepositResponse getDepositDetails(@PathVariable(value = "accountNumber") String accountNumber);

}

Now my requirement is to control them through something like a Spring Profile, so that if the profile name = "Eureka" the Eureka annotation is active and Non-Eureka is active otherwise.

I have to do it somehow in a single Interface name since I use it like the following:

private final DepositFeignClient depositFeignClient;
//other code
DepositResponse depResponse =
                    depositFeignClient.getDepositDetails(accountNumber);
//other code

Please let me know if somehow using @Profile, @ConditionalOnProperty or anything else would help solve my purpose. I am using Spring-boot 2.x and Java 8

Edit Kindly note in Eureka case I am using path and value attributes and in non-Eureka case I am using name and url attributes and that is the problem.

abhihello123
  • 1,668
  • 1
  • 22
  • 38
  • Create two properties files for each of your profiles: eureka and non-eureka (something like dev and prod maybe). `FeignClient` annotation `url` param can be interpolated from properties so you can place something like `deposite.url=127.0.0.1:` in non-eureka and `deposite.url=` in eureka profile. You don't need to have two interfaces for that. – matejko219 Mar 15 '20 at 16:58
  • @matejko219, check in case of Eureka, I am using value and path attributes (not url) and in case of non-Eureka I am using name and url attributes so can't handle like that. – abhihello123 Mar 15 '20 at 17:01

1 Answers1

1

You should try creating 2 application-{profileName}.properites files, setting the parameters as required, then running with that profile active.

Section 8 of this article may also be of use to you: https://www.baeldung.com/spring-profiles

  • I know about profile but notice I am using value and path attributes (not url) and in case of non-Eureka I am using name and url attributes so can't handle like that. – abhihello123 Mar 15 '20 at 17:03
  • @abhihello123 - you could use all 4 parameters in `@FeignClient(...)` and set required 2 as needed in configuration while keeping the other two to default values. See [documentation](https://www.javadoc.io/doc/org.springframework.cloud/spring-cloud-netflix-core/1.2.1.RELEASE/org/springframework/cloud/netflix/feign/FeignClient.html) for defaults – MartinBG Mar 15 '20 at 17:17
  • Each attribute will have a default value. You could try setting all properties in each of your configuration files with the default value for each attribute. Thus meaning that you need to specify both `value` and `path` in your annotation at all times while only selectively setting them to custom values. – NimbleNavigator Mar 15 '20 at 17:17
  • 1
    Thanks I didn't realize I could make use of the default values. Worked for me. Would post my answer soon. – abhihello123 Mar 20 '20 at 03:26
  • Glad to hear you're making some progress! :-) – NimbleNavigator Mar 20 '20 at 09:28