![](/img/trans.png)
[英]Body in serverRequest is null with ServerRequest(Spring Webflux)
[英]How to get request body from ServerRequest object in Spring WebFlux reactive?
嘗試提取使用 Postman 發送到我的 Spring 應用程序的 POST 請求的正文。 我試過做ServerRequest.bodyToMono(String.class).toProducer().peek()
但返回空值。
不再支持ServerRequest.bodyToMono(String.class).block()
。
還嘗試這樣做:
Mono<String> bodyData = request.bodyToMono(String.class);
System.out.println("\nsubscribing to the Mono.....");
bodyData.subscribeOn(Schedulers.newParallel("requestBody")).subscribe(value -> {
log.debug(String.format("%n value consumed: %s" ,value));
});
但我似乎無法在日志中顯示任何內容。
如果您正在尋找僅將主體存儲在某個緩存中的反應式休息端點的示例,則以下示例應該可以實現這一點
public class Example implements HandlerFunction<ServerResponse> {
private final CacheService cache;
@Override
public Mono<ServerResponse> handle(ServerRequest request) {
return request.bodyToMono(String.class)
.doOnNext(body -> cache.put(body))
.flatMap(body -> ServerResponse.noContent().build());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.