ClientHttpRequestInterceptor not called in springboot

自古美人都是妖i 提交于 2021-02-08 08:28:18

问题


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?


回答1:


  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 ?



来源:https://stackoverflow.com/questions/55059537/clienthttprequestinterceptor-not-called-in-springboot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!