As part of a problem I ran into detailed in this question from stackoverflow I created CompletionStageWrappingResponse for cases where a ServerResponse needs to be created in a completion stage (complete with status, headers and cookies).

Please take into consideration, that declarative webmvc has this feature with CompletionStage<ResponseEntity<T>>

I have added the whenComplete builder method to ServerResponse so now it will be possible for a user to do:

RouterFunctions.route().route(RequestPredicates.path("/helloWorld/{option}"), x -> ServerResponse.whenComplete(service.futureFoo());

where the service will be able to in an asynchronous stream decide what response it wants to offer like so

CompletionStage<ServerResponse> futureFoo(){
    return CompletableFuture.supplyAsync(()-> /* some async process returning a boolean */)
                  .thenApply(x-> x ? ServerResponse.ok().body("true") :  
                     ServerResponse.status(HttpStatus.FOUND).location(URI.create("/look/somewhere/else").build());
}

I have a use case for this behaviour in my production deployed application. As a half measure I have created a 'mycompany-spring-extras' library with this functionality.

As I don't want to find myself in trouble when in the future migrating to newer versions of spring, I humbly try to contribute this code.

Best regards, Alex.

Comment From: pivotal-issuemaster

@alexfeigin Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

Comment From: pivotal-issuemaster

@alexfeigin Thank you for signing the Contributor License Agreement!

Comment From: rstoyanchev

I believe this is a duplicate of #25828. Thanks for the pull request @alexfeigin and apologies for missing it!

Comment From: alexfeigin

Thanks @rstoyanchev I can breath easier having put a todo in my own code to move away from my patched solution to ServerResponse.async when 5.3 becomes GA.