在springboot中未调用ClientHttpRequestInterceptor

时间:2019-03-08 08:44:11

标签: httprequest spring-boot-maven-plugin

我正在尝试使用ClientHttpRequestInterceptor向我的应用程序添加日志记录。我的拦截器没有被调用。 不知道这里出了什么问题-

这是我的代码-

    @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());
    }
...................

这是我的配置文件

@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;
  }
}

这是我的WebMVC文件

@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/**/");
      }
    };
  }


}

这是我的申请文件

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

public class XManagementApplication {

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

有人可以告诉我为什么在尝试调用任何API时未调用我的拦截器类

有什么帮助吗?

1 个答案:

答案 0 :(得分:-1)

  1. 我不太了解为什么要将RestTemplateInterceptor实例化为Bean。为什么不简单地在RestTemplateConfig.restTemplate()方法中实例化您的拦截器?

    @配置     公共类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. 顺便说一句,为什么您需要将RequestContext传递给拦截器的构造函数?