title: WebTarget Bean instantiation failing in Spring while calling the contained class method through CompletableFuture
body: <p>I am calling different other web services from my spring boot application. To increase the performance rather than calling them sequentially I wanted to implement <code>CompletableFuture.supplyAsync()</code> to have an asynchronous execution.</p>
  
  <p>The services which i am calling, some of them are internal and some are external. Therefore, for internal I am directly calling their api interfaces which are present in maven dependency  and for external ones i am using <code>javax.ws.rs.client.WebTarget</code></p>
  
  <p>While implementing the <code>CompletableFuture.supplyAsync()</code> call the client classes which are internal gets executed successfully while those which have <code>WebTarget</code> as <code>@Inject</code> dependency fails as the <code>scopedTarget.XXService_nameXX_wt</code> bean is not created. </p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.charityserv_wt': Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.ws.rs.client.WebTarget]: Factory method 'target' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.coreContext': Scope 'request' is not active for the current thread
  </code></pre>
  
  <p>Calling code:</p>
  
  <pre><code>CompletableFuture&lt;SearchResult&gt; future = CompletableFuture.supplyAsync(() -&gt; searchServiceClientImpl.getsearchSearchRequest(encryptedAccountNumber));
  </code></pre>
  
  <p>ServiceClientClass: </p>
  
  <pre><code>@Component
  public class SearchServiceClientImpl{
  
      public static final Logger LOGGER = LoggerFactory.getLogger(SearchServiceClientImpl.class);
  
  
      @Inject
      @EndPoint(service = "searchserv")
      private WebTarget searchserv;
  
  
      /**  
      @Inject
      @EndPoint(service = "searchserv")
      private SearchResource searchserv;
      **/ 
  
      public Response buildsearchRequest(String payerId) throws WebApplicationException , IllegalArgumentException,ProcessingException {
  
          Response response = null;   
          String apiUrl = "path/url";
  
          response = searchserv.path(apiUrl).request(MediaType.APPLICATION_JSON).get();        
  
          return response;
      }
  
      public SearchSearchResult getsearchSearchRequest(String encryptedAccountNumber){
  
          SearchSearchResult SearchSearchResult = null;   
          Response response = null;
  
          try{
              response = buildSearchRequest(encryptedAccountNumber);
  
              if (null == response){ 
                  return null;
              } 
              if(response.getStatus() == Response.Status.OK.getStatusCode()) {                 
                  SearchSearchResult = response.readEntity(SearchSearchResult.class);
              } 
  
          return SearchSearchResult;
      }
  </code></pre>
  
  <p>The dependency injection <code>SearchResource searchserv</code> which is mentioned in comments works but WebTarget causes this exception. Is there any specific reason for this?</p>