【问题标题】:How to Send JSON to a PUT Endpoint with Spring-Webflux如何使用 Spring-Webflux 将 JSON 发送到 PUT 端点
【发布时间】:2021-09-03 11:06:03
【问题描述】:

我正在尝试使用 webflux 将 json 字符串发送到 put 端点。

例如假设我要达到的终点是http://etc:99999/put/here,我的代码如下所示:

@Service
public class someService {
  private final WebClient webClient;

  public someService(WebClient.Builder webClientBuilder) {
    this.webClient = webClientBuilder.baseUrl("http://etc:9999999/").build();
  }
  
  public Mono<Void> sendPut(String someMessage) {
      return webClient.put()
                    .uri("put/here")
                    .contentType(MediaType.APPLICATION_JSON)
                    .bodyValue(someMessage)
                    .retrieve();
  }
}

但是,这似乎根本没有达到终点。我做错了什么?

【问题讨论】:

    标签: json spring-boot http spring-webflux put


    【解决方案1】:

    问题是没有订阅我的网络客户端。 即

    Mono<void> putStream = webClient.put()
                            .uri("put/here")
                            .contentType(MediaType.APPLICATION_JSON)
                            .bodyValue(someMessage)
                            .retrieve()
                            .bodyToMono();
    
    

    不会发送 put 请求。

    但是-

    Mono<void> putStream = webClient.put()
                            .uri("put/here")
                            .contentType(MediaType.APPLICATION_JSON)
                            .bodyValue(someMessage)
                            .retrieve()
                            .bodyToMono();
    
    putStream.subscribe();
    

    会。

    这是帮助我弄清楚的 Reactor 文档的相关部分:Reactor Docs

    【讨论】:

      猜你喜欢
      • 2022-10-18
      • 2020-02-25
      • 2019-10-10
      • 2021-01-17
      • 1970-01-01
      • 2020-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多