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
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.
Ajax | Assembly Language Programming |
Borland Delphi | C & C++ |
C# | CSS |
Compiler Design | Compilers |
DHTML | Debugging |
Delphi | Fortran |
Java | Lisp |
Perl | Prolog |
Python | RPG |
Ruby | Swift |
Visual Basic | XHTML |
XML | XSL |
Deep Learning with Python by François Chollet(11940)
Hello! Python by Anthony Briggs(9399)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9364)
The Mikado Method by Ola Ellnestam Daniel Brolund(9329)
Dependency Injection in .NET by Mark Seemann(8880)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(7873)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7367)
Grails in Action by Glen Smith Peter Ledbrook(7313)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7140)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(5978)
Kotlin in Action by Dmitry Jemerov(4662)
Practical Vim (for Kathryn Amaral) by Drew Neil(3734)
Cracking the GRE Premium Edition with 6 Practice Tests, 2015 (Graduate School Test Preparation) by Princeton Review(3607)
Linux Device Driver Development Cookbook by Rodolfo Giometti(3471)
Learn Windows PowerShell in a Month of Lunches by Don Jones(3261)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(2931)
Learning Java by Patrick Niemeyer & Daniel Leuck(2877)
Learning React: Functional Web Development with React and Redux by Banks Alex & Porcello Eve(2849)
Microservices with Go by Alexander Shuiskov(2726)