liveBook · Manning

bodyToMono()

As explained in Spring in Action, Sixth Edition

bodyToMono() is a WebClient response operation that converts a response body into a Mono of a specified type.

bodyToMono() extracts a WebClient response body into a Mono of a specified type. For example, a request for one ingredient produces a Mono<Ingredient> that can receive further reactive operations. section 12.4.1

Use bodyToMono() as the final response-extraction step after defining and retrieving a request:

  • Call get(), uri(), and retrieve() to construct and execute a GET request. section 12.4.1
  • Pass the target class, such as Ingredient.class, so the response becomes a typed Mono<Ingredient>. section 12.4.1
  • Apply additional Mono operations before subscribing; without subscription, the request is not sent. section 12.4.1
  • Choose bodyToFlux() instead when the response contains multiple items. section 12.4.1

Fetching one ingredient

A complete request can be written as: Mono<Ingredient> ingredient = WebClient.create() .get() .uri("http://localhost:8080/ingredients/{id}", ingredientId) .retrieve() .bodyToMono(Ingredient.class); ingredient.subscribe(i -> { ... }); The returned Mono remains a reactive value until subscription triggers the request. section 12.4.1

Sending and accepting a response

bodyToMono() also handles responses from POST and PUT requests. A POST can send an Ingredient with bodyValue() and decode the returned resource as Mono<Ingredient>. For a PUT with an empty response payload, use bodyToMono(Void.class) and subscribe to the resulting Mono<Void>. section 12.4.2

Mono<Ingredient> ingredient = WebClient.create()
    .get()
    .uri("http://localhost:8080/ingredients/{id}", ingredientId)
    .retrieve()
    .bodyToMono(Ingredient.class);
 
ingredient.subscribe(i -> { ... });

Inspect the compact request chain showing how a URL response becomes a Mono<Ingredient> and starts only after subscription. section 12.4.1

For simple response handling, retrieve() exposes bodyToMono(). When headers or cookies must influence processing, exchangeToMono() provides a ClientResponse first, which can then be mapped to bodyToMono(). section 12.4.5

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains request chaining, typed conversion, subscription, empty responses, and the retrieve versus exchangeToMono() boundary.
Examples
Demonstrates ingredient GET, POST, PUT, collection retrieval, and response handling through exchangeToMono().
What it assumes
Assumes familiarity with WebClient request methods and introduces Mono subscription through concrete request examples.
What you can do after
Apply it via WebClient to decode single-resource and empty responses into typed Mono values.
How it connects
Connects WebClient, Mono, Flux, retrieve(), exchangeToMono(), bodyToFlux(), and reactive subscription.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.