2016-09-03 25 views
-2

Error:(65, 52) java: incompatible types: inference variable U has incompatible bounds equality constraints: akka.http.javadsl.model.HttpResponse lower bounds: com.myactors.ChatActor.ChatMessagejava的:不兼容的类型:推断变量U具有不相容边界

下面的行示出了编译错误:

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被阿卡的Http的。

我不知道它在说什么。 解决方法是什么?

+0

显示您的课ChatMessage –

+0

那么thenApplyAsync是从哪里来的? –

+0

@avy:问题不明确,你可能需要看看http://stackoverflow.com/questions/27522741/incompatible-types-inference-variable-t-has-incompatible-bounds – Shankar

回答

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.ChatMessageHttpResponse之间存在子类型关系,则这是可能的。由于失败,编译器会告诉您它找不到U的有效类型。