【发布时间】:2019-08-26 16:22:02
【问题描述】:
我正在使用第三方 REST 控制器,它接受 JSON 对象数组并返回单个对象响应。当我使用有限的 Flux 从 WebClient 发布时,代码有效(我假设,因为 Flux 完成)。
但是,当Flux 可能无限时,我该怎么做;
- 在数组块中发布?
- 捕获每个 POST 数组的响应?
- 停止
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; }
}
这是我假设第三方客户端的样子;
@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) 之后没有发送任何内容。如何 POST 下一个 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()));
}
}
【问题讨论】:
标签: java spring-webflux project-reactor reactive-streams