[英]Returning multiple Monos in Spring Webflux
I'm trying to play about with SpringBoot 2.0 and the new reactive webFlux library. 我正在尝试使用SpringBoot 2.0和新的反应式webFlux库。 I want to know how I can return the results of two calls made via the none blocking WebClient to the caller of my Springboot API.
我想知道如何将通过无阻塞WebClient进行的两次调用的结果返回给我的Springboot API的调用者。 The code I have is:
我的代码是:
@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;
}
The CombinedResult object is just a POJO: 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;
}
However if I call this the response I get back is 但是,如果我称之为我得到的回复是
{
"fasSearchResponse": {
"scanAvailable": true
},
"esSearchResponse": {
"scanAvailable": true
}
}
Rather than the contents of the SearchResponse objects. 而不是SearchResponse对象的内容。 I feel I might be missing a fundamental point of how this is supposed to work!
我觉得我可能会错过这个应该如何运作的基本要点! My thought was that because the WebClient is none blocking I can fire of the two calls to the web services and then combine them without the need for completable futures etc?
我的想法是因为WebClient没有阻塞,我可以解雇两个Web服务调用,然后将它们组合起来而不需要可完成的期货等?
Spring WebFlux doesn't support nested reactive types. Spring WebFlux不支持嵌套的响应类型。 You should instead have something like this:
你应该改为:
Mono<SearchResponse> fasMono = searchService.getSearchResults(searchTerm, "fh");
Mono<SearchResponse> esMono = searchService.getSearchResults(searchTerm, "es");
Mono<CombinedResults> results = fasMono.zipWith(esMono,
(fas, es) -> {return new CombinedResults(fas, es);});
I think that you should return a Mono of an object that represents the model responded by this action. 我认为您应该返回一个对象的Mono,该对象代表通过此操作响应的模型。 Suppose that
CombinedResults
is your model. 假设
CombinedResults
是您的模型。 This class should be something like: 这个类应该是这样的:
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
}
And, on your controller you do something like this: 并且,在您的控制器上,您执行以下操作:
@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;
}
In that way you are returning a Mono object holding what you wanted as a response. 通过这种方式,您将返回一个Mono对象,其中包含您想要的响应。 The chain of operations
fasMono.flatMap
with esMono.map
builds the CombinedResults
when both Mono emit items. 操作链
fasMono.flatMap
与esMono.map
构建CombinedResults
当两个单声道发出的物品。 This combination is rather common when trying to join two Monos into one. 当尝试将两个Monos连接成一个时,这种组合很常见。 I think you could also use
zip
operator to join the Monos. 我想你也可以使用
zip
操作符加入Monos。 All of this is unrelated to WebClient. 所有这些都与WebClient无关。 If your
getSearchResults
performs only async-nonblocking operations than everything is async-nonblocking. 如果你的
getSearchResults
只执行异步非阻塞操作,那么一切都是异步非阻塞。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.