将流式流量的WebClient帖子拆分为JSON数组 [英] Splitting a WebClient Post of a Streaming Flux into JSON Arrays
本文介绍了将流式流量的WebClient帖子拆分为JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用第三方REST控制器,该控制器接受JSON对象数组并返回单个对象响应.当我使用有限的Flux
从WebClient进行发布时,代码有效(我想是因为Flux
完成了.)
I am using a third-party REST controller which accepts an array of JSON objects and returns a single object response. When I POST from a WebClient using a limited Flux
the code works (I assume, because the Flux
completes).
但是,当Flux
可能不受限制时,我该怎么办;
However, when the Flux
is potentially unlimited, how do I;
- 以大块数组形式发布?
- 每个POST数组捕获响应吗?
- 停止传输
Flux
吗?
- POST in chunks of arrays?
- Capture the response, per POSTed array?
- Stop the transmission of the
Flux
?
这是我的豆子
public class Car implements Serializable {
Long id;
public Car() {}
public Car(Long id) { this.id = id; }
public Long getId() {return id; }
public void setId(Long id) { this.id = id; }
}
这是我假设第三方客户端的外观;
This is how I assume that the third-party client looks like;
@RestController
public class ThirdPartyServer {
@PostMapping("/cars")
public CarResponse doCars(@RequestBody List<Car> cars) {
System.err.println("Got " + cars);
return new CarResponse("OK");
}
}
这是我的代码.当我发布flux2
时,在完成时会发送一个JSON数组.但是,当我发布flux1
时,第一个take(5)
之后什么也没有发送.如何发布接下来的5个块?
And here is my code. When I POST flux2
, on completion a JSON array is sent. However, when I POST flux1
, nothing is sent after the first take(5)
. How do POST the next chunks of 5?
@Component
public class MyCarClient {
public void sendCars() {
// Flux<Car> flux1 = Flux.interval(Duration.ofMillis(250)).map(i -> new Car(i));
Flux<Car> flux2 = Flux.range(1, 10).map(i -> new Car((long) i));
WebClient client = WebClient.create("http://localhost:8080");
client
.post()
.uri("/cars")
.contentType(MediaType.APPLICATION_JSON)
.body(flux2, Car.class)
// .body(flux1.take(5).collectList(), new ParameterizedTypeReference<List<Car>>() {})
.exchange()
.subscribe(r -> System.err.println(r.statusCode()));
}
}
推荐答案
- 如何在大块数组中过帐?
查看全文