错误:(65,52)java:不兼容类型:推理变量U具有不兼容的边界 等式约束:akka.http.javadsl.model.HttpResponse 下界:com.myactors.ChatActor.ChatMessage
以下行编译显示错误:
CompletionStage<HttpResponse> httpResponse =
postChat(url,
context().system(), chatData)
.thenApplyAsync(httpResponse -> new ChatActor.ChatMessage(httpResponse,"1234"));
public static class ChatMessage{
HttpResponse httpResponse;
String name;
public ChatMessage( HttpResponse httpResponse, String name) {
this.httpResponse = httpResponse;
this.name = name;
}
public HttpResponse getHttpResponse() {
return httpResponse;
}
public String getname() {
return name;
}
}
这是HttpResponse是Akka Http。
我不知道它在说什么。 应该怎样解决它?
答案 0 :(得分:0)
方法.thenApplyAsync(httpResponse -> new ChatActor.ChatMessage(httpResponse,"1234"))
返回CompletionStage<ChatActor.ChatMessage>
而非CompletionStage<HttpResponse>
,与函数httpResponse -> new ChatActor.ChatMessage(httpResponse,"1234")
一样,您将类型HttpResponse
的实例转换为实例类型为ChatActor.ChatMessage
。
只需更改变量的类型:
CompletionStage<ChatActor.ChatMessage> httpResponse =
postChat(url, context().system(), chatData)
.thenApplyAsync(httpResponse -> new ChatActor.ChatMessage(httpResponse,"1234"));
编译器之所以不简单地说“无法将CompletionStage<ChatActor.ChatMessage>
类型的实例分配给CompletionStage<HttpResponse>
类型的变量”的原因在于应用的新类型推断通用方法thenApplyAsync
。编译器尝试查找方法的类型参数U
的类型,该类型可以使调用有效,即如果ChatActor.ChatMessage
和HttpResponse
之间存在子类型关系,则可能。由于此操作失败,编译器会告诉您它无法找到U
的有效类型。