You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Spring WebClient发送无请求体的POST请求

解决Spring WebClient发送无请求体POST请求的异常问题

你遇到的异常是因为错误地用BodyInserters.fromValue(Mono.empty())传递空请求体导致的——fromValue方法用于传递普通对象,而Mono.empty()是响应式发布者类型,两者不匹配。

以下两种方式可以正确发送无请求体的POST请求:

  • 方式一:直接省略body方法
    POST请求允许不带请求体,WebClient会自动处理为空请求体的请求:

    webClient.post()
            .uri(createUrl)
            .retrieve()
            .bodyToMono(Void.class);
    
  • 方式二:显式指定空请求体
    如果需要明确声明空请求体,可以使用BodyInserters.empty():

    webClient.post()
            .uri(createUrl)
            .body(BodyInserters.empty())
            .retrieve()
            .bodyToMono(Void.class);
    

内容的提问来源于stack exchange,提问作者user1555190

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 18:04:58