0

Is it possible to return a CompletableFuture<CompletableFuture<Result>> to the Play framework?

Below is my action code:

@BodyParser.Of(BodyParser.Json.class)
public CompletableFuture<CompletableFuture<Result>> addPageGroupMedia(){
    JsonNode payloadJson = request().body().asJson();
    PageGroupPayload payload = Json.fromJson(payloadJson, PageGroupPayload.class);
    logger.debug("PageGroupMediaPayload :: " + payload);
    Response res = new Response();
    CompletableFuture<CompletableFuture<Boolean>> cf = mediaLibraryService.addPageGroupMedia(payload);

    return cf.thenApply(data -> data.handle((d, e) -> {
        if (e != null) {
            res.setStatus(ERROR);
            logger.error("Failed to add page group media - : {}", payload, e);
        } else {
            res.setStatus(SUCCESS);
            res.setData(data);
        }
        logger.debug("res :: " + res);
        return ok(Json.toJson(res));
    }));
}

I get the following error when i execute this action:

Cannot use a method returning java.util.concurrent.CompletableFuture[java.util.concurrent.CompletableFuture[play.mvc.Result]] as a Handler for requests
Didier L
  • 18,905
  • 10
  • 61
  • 103
simplyblue
  • 2,279
  • 8
  • 41
  • 67
  • I am not familiar with that framework, but why do you want to return a nested CompletableFuture? can't you change the code to return a simple `CompletableFuture`? – user140547 Oct 24 '16 at 12:13
  • 2
    This looks like a misuse of the `CompletableFuture` API. `addPageGroupMedia()` is probably using `thenApply()` instead of [`thenCompose()`](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html#thenCompose-java.util.function.Function-), which avoids nested futures. – Didier L Oct 24 '16 at 12:32
  • This is an associated answer: http://stackoverflow.com/questions/8559537/where-does-the-flatmap-that-s-idiomatic-expression-in-scala-come-from – Steve Chaloner Oct 24 '16 at 19:50

0 Answers0