Spring 5 Web Reactive - Web Client - Use of flatmap() on the response stream

code4kix

I am trying to understand why a flatmap() is used on the response stream, in this post, for example (copy pasted directly from the reference)

final ClientRequest request = ClientRequest.GET(url)
        .accept(MediaType.TEXT_EVENT_STREAM).build();
Flux<Alert> alerts = webClient.exchange(request)
        .flatMap(response -> response.bodyToFlux(Alert.class));

AFAIK, a flatmap applied on a stream produces an arbitrary number of values (0...n) for each input value (in the stream). So a flatmap takes a function that produces a stream.

A flux emits 0 or more items, and then optionally either completing or erroring.

So what exactly is happening on the response stream? Is this the function that takes the response stream, and emits 0 or more objects of the class Alert? So if we subscribe to alerts, we can get them over the web client in a reactive manner. Can someone clarify if I am correct, please?

Simon Baslé

Yes that is correct. The receiving of the header is a first async stage, at which point you get a response. You then use flatMap to asynchronously retrieve the body. Said body is a Flux, because in some cases you can get multiple unmarshalled objects (eg SSE). Here you'd probably get only one Alert.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Spring 5 Web Reactive-Webクライアント-応答ストリームでのflatmap()の使用

分類Dev

Spring http/web forward not working with spring-reactive

分類Dev

Spring http/web forward not working with spring-reactive

分類Dev

Spring http/web forward not working with spring-reactive

分類Dev

Spring 5を使用した実際のコントローラーの例:Web Reactive

分類Dev

Spring 5を使用した実際のコントローラーの例:Web Reactive

分類Dev

Spring 5を使用した実際のコントローラーの例:Web Reactive

分類Dev

SpringのWeb Reactive Frameworkを理解する

分類Dev

Spring Web Flux Reactive + Server Sent Events =無限ループ?

分類Dev

Spring Web Flux Reactive + Server Sent Events =無限ループ?

分類Dev

Creating a Skype Web Client

分類Dev

Spring 5 Web Reactive-WebClientを使用してFluxでストリーミングデータを取得するにはどうすればよいですか?

分類Dev

Spring 5 Web Reactive-WebClientを使用してFluxでストリーミングデータを取得するにはどうすればよいですか?

分類Dev

Spring 5 Web Reactive-WebClientを使用してFluxでストリーミングデータを取得するにはどうすればよいですか?

分類Dev

Spring 5 Reactive - WebExceptionHandler is not getting called

分類Dev

Spring Reactive Web-例外は常に500でラップされます

分類Dev

How to use SSO on multiple spring security web applications without SAML?

分類Dev

HttpClient to retrieve web API response

分類Dev

How to push data over reactive websocket with Spring in response to request?

分類Dev

vSphere Web Client Object properties

分類Dev

Spring Integration http-outbound-gateway for xml web service not returning response data

分類Dev

IntelliJ + Spring Web MVC

分類Dev

Handle # character in spring web

分類Dev

Spring Web Application architecture

分類Dev

Spring Web Application architecture

分類Dev

Spring Web Application architecture

分類Dev

What was I wrong when using Jersey Client to authenticate an Spring Security web application?

分類Dev

PHP: Cannot stream CSV file to client in response to AJAX call

分類Dev

How to use Spring Security 5 and OAuth2 Client to get refresh tokens and make API calls?

Related 関連記事

  1. 1

    Spring 5 Web Reactive-Webクライアント-応答ストリームでのflatmap()の使用

  2. 2

    Spring http/web forward not working with spring-reactive

  3. 3

    Spring http/web forward not working with spring-reactive

  4. 4

    Spring http/web forward not working with spring-reactive

  5. 5

    Spring 5を使用した実際のコントローラーの例:Web Reactive

  6. 6

    Spring 5を使用した実際のコントローラーの例:Web Reactive

  7. 7

    Spring 5を使用した実際のコントローラーの例:Web Reactive

  8. 8

    SpringのWeb Reactive Frameworkを理解する

  9. 9

    Spring Web Flux Reactive + Server Sent Events =無限ループ?

  10. 10

    Spring Web Flux Reactive + Server Sent Events =無限ループ?

  11. 11

    Creating a Skype Web Client

  12. 12

    Spring 5 Web Reactive-WebClientを使用してFluxでストリーミングデータを取得するにはどうすればよいですか?

  13. 13

    Spring 5 Web Reactive-WebClientを使用してFluxでストリーミングデータを取得するにはどうすればよいですか?

  14. 14

    Spring 5 Web Reactive-WebClientを使用してFluxでストリーミングデータを取得するにはどうすればよいですか?

  15. 15

    Spring 5 Reactive - WebExceptionHandler is not getting called

  16. 16

    Spring Reactive Web-例外は常に500でラップされます

  17. 17

    How to use SSO on multiple spring security web applications without SAML?

  18. 18

    HttpClient to retrieve web API response

  19. 19

    How to push data over reactive websocket with Spring in response to request?

  20. 20

    vSphere Web Client Object properties

  21. 21

    Spring Integration http-outbound-gateway for xml web service not returning response data

  22. 22

    IntelliJ + Spring Web MVC

  23. 23

    Handle # character in spring web

  24. 24

    Spring Web Application architecture

  25. 25

    Spring Web Application architecture

  26. 26

    Spring Web Application architecture

  27. 27

    What was I wrong when using Jersey Client to authenticate an Spring Security web application?

  28. 28

    PHP: Cannot stream CSV file to client in response to AJAX call

  29. 29

    How to use Spring Security 5 and OAuth2 Client to get refresh tokens and make API calls?

ホットタグ

アーカイブ