79369727

Date: 2025-01-19 20:44:06
Score: 2
Natty:
Report link

I was able to solve my problem by adding RestClient.Builder as a parameter of the bean created in the class RestClientConfig.

So for valid handling Trace ID by RestClient in Spring Boot 3 application the class RestClientConfig should be configured in following way:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.support.RestClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;

import com.example.clients.BeClient;

@Configuration
public class RestClientConfig {

    @Value("${api.url}")
    private String apiUrl;

    @Bean
    public BeClient beClient(RestClient.Builder restClientBuilder) {

        RestClient restClient = restClientBuilder
                .baseUrl(apiUrl)
                .build();
        var restClientAdapter = RestClientAdapter.create(restClient);
        var httpServiceProxyFactory = HttpServiceProxyFactory.builderFor(restClientAdapter).build();
        return httpServiceProxyFactory.createClient(BeClient.class);

    }

}

Working source code you can find here: https://github.com/wisniewskikr/chrisblog-it-cloud/tree/main/spring-cloud/observability/springcloud-springboot3-observability-grafana-stack-restclient

Reasons:
  • Probably link only (1):
  • Contains signature (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Chris