Hi, I am using spring dependency
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
</parent>
I am using web reactive in spring web flux. I have implemented a Handler function for POST request. I want the server to return immediately. So, I have implemeted the handler as below -:
public Mono<ServerResponse> handle(ServerRequest request) {
Mono<String> bodyMono = request.bodyToMono(String.class);
bodyMono.map(str -> {
System.out.println("body got is " + str);
return str;
}).subscribe();
return ServerResponse.status(HttpStatus.CREATED).build();
}
But the print statement inside the map function is not getting called. It means the body is not getting extracted.
If I do not return the response immediately and use return bodyMono.then(ServerResponse.status(HttpStatus.CREATED).build())
, then the map function is getting called.
So, how can I do processing on my request body in the background? Please help.
Comment From: rstoyanchev
I've edited your comment to improve the formatting. You might want to check out this Mastering Markdown guide for future reference.
Comment From: rstoyanchev
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.
Comment From: rstoyanchev
As a hint, you almost never want to call .subscribe()
from a web handler. That's the equivalent of kicking off asynchronous reading of the body, while the handler method returns and indicates handling is complete. It needs to be one connected request handling chain.