1

I have a REST client interface like this:

public interface NameSearchClient {
    @RequestLine("POST")
    @Headers("Content-Type: application/json")
    SearchResponse searchByName(NameSearchRequest request);

}

It gets implemented and set-up in a factory class like this:

@Component
public class NameClientFactory {
    public <T> T createFeignClient(Class<T> clientClass, String apiUrl) {
        return Feign.builder()
                .encoder(new GsonEncoder())
                .decoder(new GsonDecoder())
                .target(clientClass, apiUrl);
    }
}

I was wondering if it's possible to write an Aspect annotation that upon every call made by that client, executes some action (another HTTP call in my case) and adds a new header to the request? Is AspectJ the right tool for that?

Phil
  • 7,065
  • 8
  • 49
  • 91

1 Answers1

1

Create a bean that implements feign.RequestInterceptor and do your work in there:

@Component
public class FeignRequestInterceptor implements RequestInterceptor {
  @Override
  public void apply(RequestTemplate template) {
    // your code here
  }
}

See also this answer.

Andy Brown
  • 11,766
  • 2
  • 42
  • 61