- Notifications
You must be signed in to change notification settings - Fork 38.9k
Description
This has been observed using spring-boot 3.2.1 with spring-web 6.1.2
When using RestClient.create(RestTemplate) and providing a RestTemplate instance that holds any HttpClientRequestInerceptors, RestClient copies the interceptor list and creates an own InterceptingClientHttpRequestFactory with the copied list.
But it also uses the InterceptingClientHttpRequestFactory obtained from RestTemplate.getRequestFactory() containing the (same) interceptors from the RestTemplate to create the requests.
Now when creating a request, a request chain containing two different instances of InterceptingClientHttpRequest each with an equal interceptor list are created and thus each interceptor is called twice during request execution.
There is a workaround, when you do not need to reuse the RestTemplate:
RestClient restClient = RestClient.create(restTemplate); restTemplate.setInterceptors(List.of());Complete code to reproduce:
@Test void restclient_should_calls_interceptors_from_resttemplate_only_once() { AtomicInteger calls = new AtomicInteger(); RestTemplate restTemplate = new RestTemplate(); restTemplate.setInterceptors(List.of((request, body, execution) -> { calls.incrementAndGet(); return execution.execute(request, body); })); RestClient restClient = RestClient.create(restTemplate); // Workaround when you do not need to reuse RestTemplate // restTemplate.setInterceptors(List.of()); try { restClient.get().uri("https://localhost:8080/foobar").retrieve(); } catch (ResourceAccessException e) { // http-call fails but does not matter. } assertEquals(1, calls.get()); }