0

I am very new to reactive programming. I am consuming a webflux API which is returning stream response ( application/stream+json ). My task is to call the API and convert response to List. My code snippet for calling API

ClientResponse res  =  webClient.method(HttpMethod.GET)
                .uri("uri")    
               .header("Authorization", "Basic " + encoding)
                .header("Accept","*/*").exchange().block();

The API response is

{
 "name" : "Andrew"
  ....
}
{
 "name" : "Bob"
  .....
}

I am trying out few examples like

Flux<String> flux = res.bodyToFlux(String.class);
List<String>> list1 = flux.collectList().block;

But this returns list1 with just a single String of the entire response but my requirement is List of multiple string corresponding to each { } in API response. Can anyone please help here.


You can use state or search in to prop of your link component

<Link
  to={{
    pathname: "/courses",
    search: "?sort=name",
    hash: "#the-hash",
    state: { fromDashboard: true }
  }}
/>

And then get it in your another component:

const { search, state } = useLocation();
4

2 回答 2

1

使用block()您切换到命令式代码。所以在这里

ClientResponse res  =  webClient.method(HttpMethod.GET)
                .uri("uri")    
               .header("Authorization", "Basic " + encoding)
                .header("Accept","*/*").exchange().block();

你正在打破反应流。您应该将其更改为:

return webClient.method(HttpMethod.GET)
            .uri("uri")    
            .header("Authorization", "Basic " + encoding)             
            .header("Accept","*/*")
            .retrieve()
            .bodyToFlux(String.class)
            .collectList()
            .block();
于 2022-01-06T10:27:45.527 回答
0

没有 block() 的通量列表

List<String> list = new ArrayList<>();
webClient.method(HttpMethod.GET)
            .uri("uri")    
            .header("Authorization", "Basic " + encoding)             
            .header("Accept","*/*")
            .retrieve()
            .bodyToFlux(String.class)
            .collectList().subscribe(list::addAll);

于 2022-01-06T10:34:33.083 回答