【问题标题】:How to send a body with HTTP DELETE when using WebFlux?使用 WebFlux 时如何使用 HTTP DELETE 发送正文?
【发布时间】:2020-06-04 23:32:19
【问题描述】:

我想访问一个提供DELETE 端点的HTTP API。这个特定的端点需要一个项目列表(我想删除它)作为 JSON 正文。

现在,我的问题是,我正在使用 Spring Webflux。但它的WebClient 并没有给我发送带有DELETE 请求的正文的可能性。对于POST,我会这样做:

webClient.post()
         .uri("/foo/bar")
         .body(...)
         .exchange()

但是对于DELETE,我得到一个RequestHeadersSpec,但我无法选择提供body(...)

webClient.delete()
         .uri("/foo/bar")
         .body(...)       <--- METHOD DOES NOT EXIST
         .exchange()

那么,在客户端使用 Spring Webflux 实现这一点的方法是什么?

【问题讨论】:

  • 根据RFC 7231A payload within a DELETE request message has no defined semantics; sending a payload body on a DELETE request might cause some existing implementations to reject the request.。因此,您正在处理一些默认情况下可能不支持的不符合标准的扩展

标签: java spring http spring-webflux


【解决方案1】:

您可以使用 webClient 的 method() 运算符。简单的例子,

return webClient
        .method(HttpMethod.DELETE)
        .uri("/delete")
        .body(BodyInserters.fromProducer(Mono.just(new JSONObject().put("body","stringBody").toString()), String.class))
        .exchange() 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2018-06-06
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多