Ank*_*Rai 6 java spring jax-rs spring-boot completable-future
我正在从我的 Spring Boot 应用程序调用不同的其他 Web 服务。为了提高性能而不是顺序调用它们,我想实现CompletableFuture.supplyAsync()
异步执行。
我正在调用的服务,其中一些是内部的,一些是外部的。因此,对于内部,我直接调用它们的 api 接口,这些接口存在于 Maven 依赖项中,而对于外部接口,我正在使用javax.ws.rs.client.WebTarget
在实现调用时,CompletableFuture.supplyAsync()
内部的客户端类会成功执行,而具有依赖关系的客户端类则会WebTarget
因未创建 bean 而@Inject
失败。scopedTarget.XXService_nameXX_wt
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
Run Code Online (Sandbox Code Playgroud)
调用代码:
CompletableFuture<SearchResult> future = CompletableFuture.supplyAsync(() -> searchServiceClientImpl.getsearchSearchRequest(encryptedAccountNumber));
Run Code Online (Sandbox Code Playgroud)
服务客户端类:
@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;
}
Run Code Online (Sandbox Code Playgroud)
SearchResource searchserv
注释中提到的依赖注入有效,但 WebTarget 会导致此异常。这有什么具体原因吗?
归档时间: |
|
查看次数: |
1336 次 |
最近记录: |