-1

Error:(65, 52) java: incompatible types: inference variable U has incompatible bounds equality constraints: akka.http.javadsl.model.HttpResponse lower bounds: com.myactors.ChatActor.ChatMessage

The following line compiles shows error :

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

Here is HttpResponse is of Akka Http.

I dont know what it is saying. What should the way to resolve it?

avy
  • 417
  • 1
  • 9
  • 21

1 Answers1

0

The method .thenApplyAsync(httpResponse -> new ChatActor.ChatMessage(httpResponse,"1234")) returns a CompletionStage<ChatActor.ChatMessage> rather than CompletionStage<HttpResponse>, as with your function httpResponse -> new ChatActor.ChatMessage(httpResponse,"1234"), you are converting the instance of type HttpResponse to an instance of type ChatActor.ChatMessage.

So simply change the variable’s type:

CompletionStage<ChatActor.ChatMessage> httpResponse =
     postChat(url, context().system(), chatData)
    .thenApplyAsync(httpResponse -> new ChatActor.ChatMessage(httpResponse,"1234"));

The reason why the compiler doesn’t simply say “cannot assign an instance of type CompletionStage<ChatActor.ChatMessage> to a variable of type CompletionStage<HttpResponse>” lies in the new type inference applied to the generic method thenApplyAsync. The compiler tries to find a type for the method’s type parameter U that could make the invocation valid, i.e. if there was a subtype relationship between ChatActor.ChatMessage and HttpResponse, it would be possible. Since this fails, the compiler tells you that it can’t find a valid type for U.

Holger
  • 285,553
  • 42
  • 434
  • 765