Spring Reactive-收集分页结果序列作为所有结果的Mono

时间:2018-07-31 14:00:24

标签: java spring spring-webflux project-reactor

在我的REST服务中,我必须多次调用另一个REST服务才能获取结果列表的所有页面。该请求包含一个from字段,我需要为每个请求增加该字段。响应包含一个totalResults字段-阅读完所有结果后,我需要停止调用其他服务,收集所有调用的所有结果并产生一个Mono<List<Result>>响应。

这是我到目前为止的情况:

@Getter
public class Request {
    private int from;
    private int size = 1000;
    private String type;

    public Request(String type, int from) {
        this.type = type;
        this.from = from;
    }
}

@Getter
@Setter
public class Response {
    private Integer totalResults;
    private Integer size;
    private Integer from;
    private List<Result> results;
}

public Mono<List<Result>> findByType(String type) {
    return Flux.generate(
            () -> new Request(type, 0),
            (Request request, SynchronousSink<List<Result>> sink) -> {
                Response response = find(request).block();
                sink.next(response.getResults());
                int nextFrom = response.getFrom() + response.getSize();
                if (nextFrom >= response.getTotalResults()) {
                    sink.complete();
                }
                return new Request(type, nextFrom);
            })
            .flatMap(Flux::fromIterable)
            .collectList();
}

private Mono<Response> find(Request request) {
    return webClient
            .post()
            .uri("/search")
            .syncBody(request)
            .retrieve()
            .bodyToMono(Response.class);
}

它可以在使用MockWebServerStepVerifier的测试中工作,但在生产时无法使用

java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-2

我该如何采取正确的反应方式?

1 个答案:

答案 0 :(得分:4)

EDIT ,在Adam的帮助下,self.myImageView?.layer.cornerRadius = (self.myImageView?.frame.size.width)! / 2; self.myImageView?.clipsToBounds = true 功能解决了此问题

{{1}}