struggling on this strange situation from a while. I've wrote a library, who define a feign client, to consume a bunch of API. This client definition is a copy-paste code I always take from previous projects, so a working code, but the difference is that this time, is included into a library.
@Bean
public DcApi buildApiManagement(@Autowired ObjectMapper mapper,.....) {
.....
return Feign.builder()
.contract(new SpringMvcContract())
.encoder(new JacksonEncoder(mapper))
.decoder(new JacksonDecoder(mapper))
.logger(new Slf4jLogger(DcApi.class))
.logLevel(Logger.Level.BASIC)
.requestInterceptor(new OAuthInterceptor(x509app, oAuth2Scope))
.target(DcApi.class, url);
}
The interceptor, is pretty simple:
public static class OAuthInterceptor implements RequestInterceptor {
private final X509Application x509Application;
private final String scope;
public OAuthInterceptor(X509Application x509Application, String scope) {
this.x509Application = x509Application;
this.scope = scope;
log.info("Building OAuthInterceptor.");
}
@Override
public void apply(RequestTemplate template) {
template.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
template.header(HttpHeaders.AUTHORIZATION, "Bearer " + this.x509Application.getAccessToken(this.scope));
}
}
When it comes to use the library in the application, if I go in debug mode with a breakpoint in the feign builder, I see the interceptor is correctly created (and it works, if I call direct methods on it from the debugger). Anyway, when it comes to use the API, the interceptor looks empty.
I've no clue why this is happening, is anyone can help me to figure out?
The API in the library user application, is @Autowired from a service class.
I'm using spring-cloud-starter-openfeign-4.1.0 and org.springframework.boot-3.2.2
Thanks to everyone :)
I've tried to move the definition of the OAuthInterceptor from the static class to a @Bean, but no success. I've added log into the feignclient definition to be sure that is created one time only, cause before, having it marked with @Configuration, brought to a double creation.
Turned out the problem was due to the way I handled the interceptor. Changing the implementation to
Doing this, together with the modification of the X509Application as a bean, solved the situation.
I suspect the behavior happened because, together with this new feign client I was about to create, there was also another one in the library, with an interceptor defined as the solution proposed: I think the 2 way of definition, entered in some kind of overflow.