Beware of using spring WebClient(Episode7)
This episode story is simple.
If we receive response that is list type, we can use flatMapIterable
operator(finally return type is Flux). Sometimes, I found the Mono<List<T>>
type.
public Flux<String> getData() {
return this.webClient.get()
.uri(uriBuilder -> uriBuilder
.path("/data")
.build()
)
.retrieve()
.bodyToMono(DataApiResponse.class)
.flatMapIterable(DataApiResponse::getContent);
}
@Value
public class DataApiResponse {
List<String> content;
}
Afterwards, If we use getData()
method in the business layer, we can change Flux to Stream using toStream()
operator and then change Stream to List using collect
operator again.
List<String> data = apiClient.getData()
.toStream()
.collect(toUnmodifiableList());