gpt4 book ai didi

java - 添加一层 thenApply 后出现不兼容类型 : bad return type in lambda expression X cannot be converted to CompetionStage,

转载 作者:行者123 更新时间:2023-11-30 06:10:43 27 4
gpt4 key购买 nike

我收到以下错误

[ERROR] AccountServiceResource.java:[165,38] incompatible types: bad return type in lambda expression
[ERROR] Response<okio.ByteString> cannot be converted to java.util.concurrent.CompletionStage<Response<okio.ByteString>>

关于下面一行

return checkExceptionCauses(exception);

哪里checkedExceptionCauses是一个返回 Response<ByteString> 的方法

private Response<ByteString> checkExceptionCauses(Throwable exception) {
// ...
}

问题是为什么它试图将其转换为 CompletionStage<>突然间?这是编译良好的原始代码(简化版本):

private CompletionStage<Response<ByteString>> getAccountById(RequestContext rc) {
return accountServiceClient.getAccount().thenApply( getAccountResponse -> {
AdAccountResponse payload;
payload.map(getAccountResponse);
return Response.forPayload(serializePayload(payload));
}).exceptionally(exception -> {
LOG.error("Lorem ipsum");
return checkExceptionCauses(exception);
});
}

所以你看,我们返回了 .thenApply() 的任何内容。返回,或 .exceptionally() 。 (诚​​然,我不太熟悉可完成的 future ,所以可能这就是我在这里感到困惑的原因。)

但是好吧,我觉得我的修改做了同样的事情:

private CompletionStage<Response<ByteString>> getAccountById(RequestContext rc) {
return accountServiceClient.getAccount().thenApply( getAccountResponse -> {
AdAccountResponse payload;
payload.map(getAccountResponse);

// *** BEGIN CHANGES *** //
Request salesforceRequest = Request.forUri(FORCEIT_GET_BUSINESS_INFO_URI, "GET").withPayload(businessInfoRequestPayload);
return httpClient.send(salesforceRequest, rc).thenApply(salesforceResponse -> {
if (salesforceResponse.payload().isPresent()) {
// ...
} else {
// ...
}
AdAccountResponse payload;
payload.map(getAccountResponse);
return Response.forPayload(serializePayload(payload));
});
// *** END CHANGES *** //

}).exceptionally(exception -> {
LOG.error("Lorem ipsum");
return checkExceptionCauses(exception);
});
}

我所做的只是添加另一层 .thenApply() 。但我有我的内心.thenApply()返回与原始代码返回的内容相同的内容,以及我的外部 .thenApply()只是传递它。

那么为什么我现在突然收到关于转换为 CompletionStage 的投诉?我尝试这个只是为了好玩:

return CompletableFuture.completedFuture(checkExceptionCauses(exception));

毫不奇怪,我现在收到了上级关于退回 CompletionStage<Response<ByteString>> 的投诉。而不是Response<ByteString> .

最佳答案

如果您有同步映射函数,则使用

thenApply

根据Documentation :

Returns a new CompletionStage that, when this stage completes normally, is executed with this stage's result as the argument to the supplied function.

另一方面,如果您有一个返回 CompletableFuture 的异步映射函数,则使用 thenCompose。换句话说,thenCompose 直接返回带有结果的 future,而不是嵌套的 future。

来自Documentation :

Returns a new CompletionStage that is completed with the same value as the CompletionStage returned by the given function. When this stage completes normally, the given function is invoked with this stage's result as the argument, returning another CompletionStage. When that stage completes normally, the CompletionStage returned by this method is completed with the same value.

所以尝试用 thenCompose 替换 thenApply

关于java - 添加一层 thenApply 后出现不兼容类型 : bad return type in lambda expression X cannot be converted to CompetionStage<X>,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50299739/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com