Łukasz Biedak
Łukasz Biedak

Reputation: 43

How can I get name of the Feign client?

I have simple interface:

public interface ServiceClient {
  String REFRESH_ENDPOINT = "/admin/refresh";
  ResponseEntity<String> refresh();
}

and few FeignClients like this:

@FeignClient(name = "${ServiceA.name}", configuration = 
FeignConfiguration.class, decode404 = true)
public interface ServiceA extends ServiceClient {

  @PostMapping(path = REFRESH_ENDPOINT)
  ResponseEntity<String> refresh();
}

Then I do:

private final List<ServiceClient> services;
...
services.parallelStream()
            .collect(Collectors.toMap(s-> s.getClass().getName(), s-> s.refresh().getStatusCode()));

How I can get name of the Feign client? getClass().getName() gives me Proxy154. I would prefer to not create static field in every FeignClient I have.

Upvotes: 2

Views: 2742

Answers (1)

pvpkiran
pvpkiran

Reputation: 27078

You can use AopProxyUtils.

services.parallelStream()
            .collect(Collectors.toMap(s-> 
                  AopProxyUtils.proxiedUserInterfaces(greetingClient)[0].getName(), 
                  s-> s.refresh().getStatusCode()));  

Not sure if you want the class name or the name you have in @FeignClient annotation. If you want the name in Annotation, then you can get it like this

Class<?>[] classes = AopProxyUtils.proxiedUserInterfaces(greetingClient);
FeignClient annotation = classes[0].getAnnotation(FeignClient.class);
System.out.println(annotation.name());

Upvotes: 3

Related Questions