Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value from CompletionStage in java

I am using play2.5 with java 8. I am making POST request to server using

WSRequest request = ws.url("http://abababa .com");
WSRequest complexRequest = request.setHeader("X-API-Key", "xxxxxx")
            .setHeader("Content-Type", "application/x-www-form-urlencoded")
CompletionStage<WSResponse> responsePromise = complexRequest.post("grant_type=password"
            + "&username=xxxxx&password=yyyyy");
CompletionStage<JsonNode> jsonPromise = responsePromise.thenApply(WSResponse::asJson);

How do I print the final response of the response. I want to return part of the response to this function. Should the function which called this function also have different code compared to synchronous code?

like image 962
raju Avatar asked Aug 12 '16 17:08

raju


People also ask

What is Completion Stage?

The stage of completion is an indicator of progress and determines for an operation, work package, or project the ratio of the delivered output to the planned total output. As a percentage, it expresses the proportion of work performed that is part of an agreed total output.

What is a CompletionStage in java?

A stage of a possibly asynchronous computation, that performs an action or computes a value when another CompletionStage completes. A stage completes upon termination of its computation, but this may in turn trigger other dependent stages.

What is completed stage method of CompletableFuture interface?

Returns a new CompletableFuture that is asynchronously completed by a task running in the given executor with the value obtained by calling the given Supplier. Returns a new CompletionStage that, when this stage completes normally, is executed with this stage's result as the argument to the supplied action.


2 Answers

jsonPromise.toCompletableFuture().get()

like image 127
the8472 Avatar answered Sep 20 '22 15:09

the8472


JsonNode jsonData = jsonPromise.toCompletableFuture().get()

I tried the code above but i get compiler error, to return JsonNode data, then i used

JsonNode jsonData = jsonPromise.toCompletableFuture().join()

and it works fine

like image 44
brynetwork Avatar answered Sep 22 '22 15:09

brynetwork