WebTarget Bean实例化在Spring中失败,同时通过CompletableFuture调用包含的类方法

问题描述 投票:4回答:1

我正在从Spring Boot应用程序调用其他Web服务。为了提高性能而不是顺序调用它们,我想实现CompletableFuture.supplyAsync()以具有异步执行。

我正在调用的服务,其中有些是内部的,有些是外部的。因此,对于内部,我直接调用其在maven依赖项中存在的api接口,对于外部,我正在使用javax.ws.rs.client.WebTarget

实现CompletableFuture.supplyAsync()调用时,内部客户端类成功执行,而具有WebTarget作为@Inject依赖项的客户端类失败,因为未创建scopedTarget.XXService_nameXX_wt bean。

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

电话代码:

CompletableFuture<SearchResult> future = CompletableFuture.supplyAsync(() -> searchServiceClientImpl.getsearchSearchRequest(encryptedAccountNumber));

ServiceClientClass:

@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;
    }

注释中提到的依赖项注入SearchResource searchserv有效,但是WebTarget导致此异常。是否有任何特定原因?

java spring spring-boot jax-rs completable-future
1个回答
0
投票

我正在检查代码库,并遇到了@Endpoint(Service =“ xyzserv”)注释。你能告诉我@Inject如何工作吗?我无法弄清楚。

© www.soinside.com 2019 - 2024. All rights reserved.