Как выполнить ClientHttpRequestInterceptor последним в RestTemplate (Spring) - PullRequest
0 голосов
/ 27 сентября 2018

У меня есть несколько ClientHttpRequestInterceptor, настроенный как моя общая конфигурация для моего RestTemplate (CommonRestTemplateBuilder), одним из которых является LogRequestInterceptor.

Проблема заключается в том, что я добавляю новый ClientHttpRequestInterceptor в новый @Configuration, в котором я создаю другой RestTemplate, используя в качестве основы commonRestTemplateBuilder, перехватчик для ведения журнала выполняется перед новым перехватчиком.

Проблема здесь в том, что новый перехватчик добавляет заголовки, которые не отражены в журнале.

Можно ли дать перехватчикам порядок?

Это код:

/**
 * Configures the common rest template builder for building {@link RestTemplate}
 */
@Bean
public RestTemplateBuilder commonRestTemplateBuilder(HttpClient httpClient) {

    return new RestTemplateBuilder()
            .requestFactory(() -> {
                //Use Apache Http Client request factory
                HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
                //Use the apache caching http client
                requestFactory.setHttpClient(httpClient);
                //Use buffering client for debugging (get the body of the request)
                return new BufferingClientHttpRequestFactory(requestFactory);
            })
            .customizers()
            .interceptors(
                    //For adding common headers
                    new AddCommonsHeadersRequestInterceptor(),
                    //For logging the requests
                    new LogRequestInterceptor());
}

Затем в другом классе:

/**
 * Configures the {@link RestTemplate} for {@link CompanyAClient}
 * using as a base the {@link CommonRestTemplateConfiguration}.
 */
@Bean
public RestTemplate companyARestTemplate() {

    return commonRestTemplateBuilder
            .rootUri("http://companyA.com/api")
            .additionalInterceptors(new AddCompanyAHeadersRequestInterceptor())
            .build();
}

Большое спасибо.

...