在Spring Webflux中返回多个Monos

我正在尝试使用SpringBoot 2.0和新的反应式webFlux库.我想知道如何将通过无阻塞WebClient进行的两次调用的结果返回给我的Springboot API的调用者.我的代码是:

@RequestMapping("/search")
public CombinedResults perfomSearch(@RequestParam final String searchTerm) {
    Mono<SearchResponse> fasMono = searchService.getSearchResults(searchTerm, "fh");
    Mono<SearchResponse> esMono = searchService.getSearchResults(searchTerm, "es");
    CombinedResults combinedResults = new CombinedResults(fasMono, esMono);
    return combinedResults;

}

CombinedResult对象只是一个POJO:

public class CombinedResults {

private Mono<SearchResponse> fasSearchResponse;

private Mono<SearchResponse> esSearchResponse;

public CombinedResults(final Mono<SearchResponse> fasSearchResponse, final Mono<SearchResponse> esSearchResponse) {
    this.fasSearchResponse = fasSearchResponse;
    this.esSearchResponse = esSearchResponse;
}

public Mono<SearchResponse> getFasSearchResponse() {
    return fasSearchResponse;
}

public void setFasSearchResponse(final Mono<SearchResponse> fasSearchResponse) {
    this.fasSearchResponse = fasSearchResponse;
}

public Mono<SearchResponse> getEsSearchResponse() {
    return esSearchResponse;
}

public void setEsSearchResponse(final Mono<SearchResponse> esSearchResponse) {
    this.esSearchResponse = esSearchResponse;
}

但是,如果我称之为我得到的回复是

{
  "fasSearchResponse": {
    "scanAvailable": true
  },
  "esSearchResponse": {
    "scanAvailable": true
  }
}

而不是SearchResponse对象的内容.我觉得我可能会错过这个应该如何运作的基本要点!我的想法是因为WebClient没有阻塞,我可以解雇两个Web服务调用,然后将它们组合起来而不需要可完成的期货等?

最佳答案
我认为您应该返回一个对象的Mono,该对象代表通过此操作响应的模型.假设CombinedResults是您的模型.这个类应该是这样的:

public class CombinedResults {

    private SearchResponse fasSearchResponse;

    private SearchResponse esSearchResponse;

    public CombinedResults(final SearchResponse fasSearchResponse, final SearchResponse esSearchResponse) {
        this.fasSearchResponse = fasSearchResponse;
        this.esSearchResponse = esSearchResponse;
    }

    //... getters AND/OR setters
}

并且,在您的控制器上,您执行以下操作:

@RequestMapping("/search")
public Mono<CombinedResults> perfomSearch(@RequestParam final String searchTerm) {
    Mono<SearchResponse> fasMono = searchService.getSearchResults(searchTerm, "fh");
    Mono<SearchResponse> esMono = searchService.getSearchResults(searchTerm, "es");
    Mono<CombinedResults> combinedResults = 
        fasMono
          .flatMap(fh -> esMono.map(es -> new CombinedResults(fh, es)));
    return combinedResults;
}

通过这种方式,您将返回一个Mono对象,其中包含您想要的响应.当Mono发出项时,带有esMono.map的操作链fasMono.flatMap构建CombinedResults.当尝试将两个Monos连接成一个时,这种组合很常见.我想你也可以使用zip操作符加入Monos.
所有这些都与WebClient无关.如果你的getSearchResults只执行异步非阻塞操作,那么一切都是异步非阻塞.

转载注明原文:在Spring Webflux中返回多个Monos - 代码日志