java - 使用 WebFlux 时如何使用 HTTP DELETE 发送正文?
问题描述
我想访问一个提供DELETE
端点的 HTTP API。这个特定的端点需要一个项目列表(我想删除)作为 JSON 正文。
现在,我的问题是,我正在使用 Spring Webflux。但是它的WebClient并没有给我发送带有DELETE
请求的正文的可能性。对于 a POST
,我会这样做:
webClient.post()
.uri("/foo/bar")
.body(...)
.exchange()
但是对于DELETE
,我得到一个RequestHeadersSpec没有给我提供一个选项body(...)
:
webClient.delete()
.uri("/foo/bar")
.body(...) <--- METHOD DOES NOT EXIST
.exchange()
那么,在客户端使用 Spring Webflux 实现这一点的方法是什么?
解决方案
您可以使用 webClient 的method()
运算符。简单的例子,
return webClient
.method(HttpMethod.DELETE)
.uri("/delete")
.body(BodyInserters.fromProducer(Mono.just(new JSONObject().put("body","stringBody").toString()), String.class))
.exchange()
推荐阅读
- rx-java - 一个 Observable 应该基于另一个 Observable 发射的项目发射项目,并且两个 Observable 都没有连接
- asp.net-core-2.1 - ef core中查询相关数据的问题
- sql-server - 如何查询父重复节点内的重复 XML 子节点?
- rest - Godaddy API 是否返回不良结果?
- vbscript - 这是一段代码,每分钟将文件夹中的所有 .csv 文件转换为 .xls。但我寻求一些帮助以避免重复
- ios - 在 Objective-C 中检查给定字符是否是表情符号
- android - 限制支持蓝牙 LE 广告的设备在 Play 商店中的 Android 应用
- php - 优化查询 - 如何选择最高和最低行,从日期时间列按日期分组并返回数组
- python - 具有 Softmax 输出层的二元分类 NN 预测不正确
- javascript - if条件下的异步操作