Spring Boot 3 Recipes by 2024

Spring Boot 3 Recipes by 2024

Author:2024
Language: eng
Format: epub


Chapter 4 WebFlux

As the WebClient reuses parts of the same infrastructure as the rest of WebFlux, we can reuse the same exception handling we used earlier. When calling http://

localhost:8090/books/9780618260300 from the command line using curl or HTTPie, you will get output similar to Figure 4-15.

Figure 4-15. Enriched book result

Use a Declarative Client

Spring Framework 6 introduced the declarative HTTP client. With this it is possible to write an interface with some annotations. At runtime a proxy will be created that will handle the request/response including the mapping and exception handling.

The starting point is an interface with an @HttpExchange annotation (or one of the derived annotations like @GetExchange and @PostExchange) on either the class or the method(s). It functions much in the same way as the @RequestMapping annotation from the server API.

Using the @HttpExchange annotation, it is possible to set some properties (see Table 4-9).

227

Chapter 4 WebFlux

Table 4-9. HttpExchange Properties

Attribute

Description

value or url

the url for the request; can be a full url, a path relative to the type level

@HttpExchange, or the preset base urI.

Method

the http method to use; DELETE, GET, PATCH, POST, or PUT. the default is

empty.

contentType

the media type to send for the Content-Type header; the default is empty.

Accept

the media type for the Accept header.

In addition to the @HttpExchange annotation, there are @DeleteExchange,

@GetExchange, @PatchExchange, @PostExchange, and @PutExchange annotations.

These can be used instead of @HttpExchange with a specific method value. Using these annotations makes it more explicit.

Just like request handling methods with @RequestMapping, the declarative client supports multiple arguments, annotations, and return types. It reuses common parts of the web infrastructure for this. See Table 4-10.

228

Chapter 4 WebFlux

Table 4-10. HttpExchange-Supported Method Arguments

Type

Description

java.net.URI

Dynamically sets the url for the request; overrides the url attribute from

the @HttpExchange.

UriBuilderFactory provides a UriBuilderFactory to expand the urI template and urI variables. used instead of the UriBuilderFactory of the underlying client.

HttpMethod

Dynamically sets the http method to use for the request; overrides the

method attribute from the @HttpExchange.

MultipartFile

adds a request part from a MultipartFile, typically used with Spring

MVC to indicate an uploaded file.

@CookieValue

adds cookies to the outgoing request. the argument can be a Map<String,

?> or a MultiValueMap<String, ?> with multiple cookies or individual

values. uses the type conversion for non-String values.

@RequestHeader

adds request headers to the outgoing request. the argument can be a

Map<String, ?> or a MultiValueMap<String, ?> with multiple

headers or individual values. uses the type conversion for non-String values.

@PathVariable

adds a variable to expand a placeholder in the request url. the argument

can be a Map<String, ?> for multiple values or an individual value.

uses the type conversion for non-String values.

@RequestBody

provides the body of the request either as an Object to be serialized or as

a reactive Streams publisher such as Mono, Flux, or any other async type

supported through the configured ReactiveAdapterRegistry.

@RequestParam

adds request parameters. Can be a Map<String, ?> /

MultiValueMap<String, ?> for multiple values. Or can be an

individual type. uses the type conversion for non-String values. When

contentType is application/x-www-form-urlencoded, the

parameters are added to the request body.

@RequestPart

adds a request part that can be a String (becomes a form field), a

Resource, an Object to be encoded as JSON (or whatever



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.