title: Spring Boot WebClient.Builder bean usage in traditional servlet multi threaded application
body: <p>I would like to have a http client to call other microservice from Spring Boot <strong>not reactive</strong> application. Because of RestTemplate will be deprecated I tried to use WebClient.Builder and WebClient. Though I not sure about thread safety. Here example:</p>
  
  <pre><code>@Service
  public class MyService{
      @Autowired
      WebClient.Builder webClientBuilder;
  
      public VenueDTO serviceMethod(){
          //!!! This is not thread safe !!!
          WebClient webClient = webClientBuilder.baseUrl("http://localhost:8000").build();
  
          VenueDTO venueDTO = webClient.get().uri("/api/venue/{id}", bodDTO.getBusinessOutletId()).
                  retrieve().bodyToMono(VenueDTO.class).
                  blockOptional(Duration.ofMillis(1000)).
                  orElseThrow(() -&gt; new BadRequestException(venueNotFound));
                  return VenueDTO;
      }
  }
  </code></pre>
  
  <p>serviceMethod() in this example will be called from few threads, and webClientBuilder is a single bean instance. The WebClient.Builder class contains state: baseUrl, and this seems not thread safe as few threads could call this state update simultaneously. Meanwhile WebClient itself seems is thread safe as mentioned in answer at <a href="https://stackoverflow.com/questions/49095366/right-way-to-use-spring-webclient-in-multi-thread-environment">Right way to use Spring WebClient in multi-thread environment</a> </p>
  
  <p>Should I use WebClient.Builder bean as mentioned in <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-webclient.html" rel="noreferrer">https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-webclient.html</a></p>
  
  <blockquote>
    <p>Spring Boot creates and pre-configures a WebClient.Builder for you; it
    is strongly advised to inject it in your components and use it to
    create WebClient instances.</p>
  </blockquote>
  
  <p>One of workaround options I see is to create WebClient without any state passed to builder so instead of:</p>
  
  <pre><code>WebClient webClient = webClientBuilder.baseUrl("http://localhost:8000").build();
  </code></pre>
  
  <p>I will do:</p>
  
  <pre><code>WebClient webClient = webClientBuilder.build();
  </code></pre>
  
  <p>and pass full url with protocol and port in uri method call:</p>
  
  <pre><code>webClient.get().uri("full url here", MyDTO.class)
  </code></pre>
  
  <p>What is the proper way to use it in my case?</p>