Request and response body conversion

In Chapter 10, Implementing MVC Pattern in a Web Application with Spring, we discussed message conversion for the request body and response body either from Java to JSON, or from JSON to Java object, and many more. Similarly, conversion is also required in the case of a Reactive web application, . The spring core module provides reactive Encoder and Decoder to enable the serialization of a Flux of bytes to and from the typed objects.

Let's see the following example for request body type conversions. Developers do not need to forcefully do type conversion--the Spring Framework automatically converts it for you in both types of approaches: Annotation-based programming, and functional-based programming.

  • Account account: This means that the account object is deserialized before the controller is called without blocking.
  • Mono<Account> account: This means that AccountController can use the Mono to declare logic. The account object is first deserialized, and then this logic is executed.
  • Flux<Account> accounts: This means that AccountController can use Flux in case of the input streaming scenario.
  • Single<Account> account: This is very similar to the Mono, but here the Controller uses RxJava.
  • Observable<Account> accounts: This is also very similar to Flux, but in this case, the Controller uses input streaming with RxJava.

In the preceding list, you saw the Spring Framework for type conversion in the reactive programing model. Let's see the following return types in the example for the response body:

  • Account: This serializes without blocking the given Account; implies a synchronous, non-blocking controller method.
  • void: This is specific to the annotation-based programming model. Request handling completes when the method returns; this implies a synchronous, non-blocking controller method.
  • Mono<Account>: This serializes without blocking the given Account when the Mono completes.
  • Mono<Void>: This implies that request handling completes when the Mono completes.
  • Flux<Account>: This is used in the streaming scenario, possibly, the SSE depends on the requested content type.
  • Flux<ServerSentEvent>: This enables SSE streaming.
  • Single<Account>: The same, but uses RxJava.
  • Observable<Account>: The same, but uses the RxJava Observable type.
  • Flowable<Account>: The same, but uses the RxJava 2 Flowable type.

In the preceding list, you have seen the return types of the handler methods. The Spring Framework does type conversions in the reactive programing model.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset