簡體   English   中英

如何從 Spring WebFlux 反應式中的 ServerRequest 對象獲取請求正文?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM