2

I am trying to add logging to my application using ClientHttpRequestInterceptor.My interceptor is not being called. Not sure what is going wrong here -

Here is my code -

    @Component
    @Slf4j
    public final class RestTemplateInterceptor implements ClientHttpRequestInterceptor {


      protected static final LoggingAspect aspect = new LoggingAspect();
      private final RequestContext requestContext;
      private boolean logResponseBody = true;


      public RestTemplateInterceptor(RequestContext requestContext) {
        this.requestContext = requestContext;
      }


      @Override
      public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
        populateHeader(request);
        traceRequest(request, body);
        ClientHttpResponse response = clientHttpRequestExecution.execute(request,body);
        traceResponse(response);
        return response;
      }


      private void populateHeader(HttpRequest request) {
        final HttpHeaders headers = request.getHeaders();

        // Propagate TAM headers
        headers.add("iv-user", requestContext.getUser());
        headers.add("MessageId", requestContext.getMessageId());
        headers.add("CorrelationId", requestContext.getConversationId());
        headers.add("BusinessId", requestContext.getBusinessId());
        headers.add("ApplicationName", requestContext.getSourceSystem());
        headers.add("iv-groups", requestContext.getGroups());
        headers.add("MessageDateTime", requestContext.getSourceTimestamp());
    }
...................

Here is my config file

@Configuration
public class RestTemplateConfig {
  /**
   * initialise restTemplate
   *
   * @param restTemplateInterceptor autowired in RestTemplateInterceptor
   * @return
   */
  @Bean
  public RestTemplate restTemplate(ClientHttpRequestInterceptor restTemplateInterceptor, ObjectMapper objectMapper) {

    RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));

    List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
    if (CollectionUtils.isEmpty(interceptors)) {
      interceptors = new ArrayList<>();
    }

    interceptors.add(restTemplateInterceptor);
    restTemplate.setInterceptors(interceptors);
    return restTemplate;
  }
}

Here is my WebMVC file

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

  @Bean
  public WebMvcConfigurer webAuthentication() {
    return new WebMvcConfigurer() {
      @Override
      public void addInterceptors(InterceptorRegistry registry) {
        //registry.addInterceptor(myInterceptor());
        registry.addInterceptor(new MVCLoggingInterceptor()).addPathPatterns("/api/**");
        registry.addInterceptor(new WebAuthentication()).addPathPatterns("/api/**/");
      }
    };
  }


}

Here is my application file

@EnableAsync
@EnableScheduling
@SpringBootApplication(exclude = { SecurityAutoConfiguration.class })

public class XManagementApplication {

  public static void main(String[] args) {
    SpringApplication.run(XManagementApplication.class, args);
  }
}

Can anybody tell why my interceptor class is not called when I try to call any API

Any help would be appreciate?

user327126
  • 965
  • 3
  • 11
  • 22
  • can anybody help me with this? – user327126 Mar 12 '19 at 00:03
  • Forgot to mention I am excluding spring security @EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class}) @SpringBootApplication public class XManagementApplication { – user327126 Mar 12 '19 at 05:10
  • On 'RestTemplateConfig' you have the following comment: "@param restTemplateInterceptor autowired in RestTemplateInterceptor" Where and how are you passing the restTemplateInterceptor parameter? It seems that it may be missing. – Kuikiker Mar 12 '19 at 09:23
  • @user327126 how do you call your api? where is the code sitting? how do you autowire your RestTemplate? Are you sure you are injecting the same RestTemplate that you are instantiating in your configurer? – xbmono Jun 02 '19 at 22:10
  • 1
    @user327126 did you get it working? I am also facing the same issue. – dassum Jul 22 '19 at 16:47

1 Answers1

0
  1. I don't really understand why you want to instantiate your RestTemplateInterceptor as a Bean. Why not simply instantiate your interceptor inside the method RestTemplateConfig.restTemplate() ?

    @Configuration
    public class RestTemplateConfig {
    
      @Bean
      public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
    
        List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
        if (CollectionUtils.isEmpty(interceptors)) {
            interceptors = new ArrayList<>();
        }
    
        interceptors.add(new RestTemplateInterceptor());
        restTemplate.setInterceptors(interceptors);
        return restTemplate;
      }
    }
    
  2. Btw, why do you need to pass RequestContext to the constructor of your interceptor ?

Shashi
  • 12,487
  • 17
  • 65
  • 111
Sébastien PRAT
  • 439
  • 4
  • 12