121 lines
2.6 KiB
Plaintext
121 lines
2.6 KiB
Plaintext
[[webflux-client-retrieve]]
|
|
= `retrieve()`
|
|
|
|
The `retrieve()` method can be used to declare how to extract the response. For example:
|
|
|
|
[tabs]
|
|
======
|
|
Java::
|
|
+
|
|
[source,java,indent=0,subs="verbatim,quotes"]
|
|
----
|
|
WebClient client = WebClient.create("https://example.org");
|
|
|
|
Mono<ResponseEntity<Person>> result = client.get()
|
|
.uri("/persons/{id}", id).accept(MediaType.APPLICATION_JSON)
|
|
.retrieve()
|
|
.toEntity(Person.class);
|
|
----
|
|
|
|
Kotlin::
|
|
+
|
|
[source,kotlin,indent=0,subs="verbatim,quotes"]
|
|
----
|
|
val client = WebClient.create("https://example.org")
|
|
|
|
val result = client.get()
|
|
.uri("/persons/{id}", id).accept(MediaType.APPLICATION_JSON)
|
|
.retrieve()
|
|
.toEntity<Person>().awaitSingle()
|
|
----
|
|
======
|
|
|
|
Or to get only the body:
|
|
|
|
[tabs]
|
|
======
|
|
Java::
|
|
+
|
|
[source,java,indent=0,subs="verbatim,quotes"]
|
|
----
|
|
WebClient client = WebClient.create("https://example.org");
|
|
|
|
Mono<Person> result = client.get()
|
|
.uri("/persons/{id}", id).accept(MediaType.APPLICATION_JSON)
|
|
.retrieve()
|
|
.bodyToMono(Person.class);
|
|
----
|
|
|
|
Kotlin::
|
|
+
|
|
[source,kotlin,indent=0,subs="verbatim,quotes"]
|
|
----
|
|
val client = WebClient.create("https://example.org")
|
|
|
|
val result = client.get()
|
|
.uri("/persons/{id}", id).accept(MediaType.APPLICATION_JSON)
|
|
.retrieve()
|
|
.awaitBody<Person>()
|
|
----
|
|
======
|
|
|
|
To get a stream of decoded objects:
|
|
|
|
[tabs]
|
|
======
|
|
Java::
|
|
+
|
|
[source,java,indent=0,subs="verbatim,quotes"]
|
|
----
|
|
Flux<Quote> result = client.get()
|
|
.uri("/quotes").accept(MediaType.TEXT_EVENT_STREAM)
|
|
.retrieve()
|
|
.bodyToFlux(Quote.class);
|
|
----
|
|
|
|
Kotlin::
|
|
+
|
|
[source,kotlin,indent=0,subs="verbatim,quotes"]
|
|
----
|
|
val result = client.get()
|
|
.uri("/quotes").accept(MediaType.TEXT_EVENT_STREAM)
|
|
.retrieve()
|
|
.bodyToFlow<Quote>()
|
|
----
|
|
======
|
|
|
|
By default, 4xx or 5xx responses result in an `WebClientResponseException`, including
|
|
sub-classes for specific HTTP status codes. To customize the handling of error
|
|
responses, use `onStatus` handlers as follows:
|
|
|
|
[tabs]
|
|
======
|
|
Java::
|
|
+
|
|
[source,java,indent=0,subs="verbatim,quotes"]
|
|
----
|
|
Mono<Person> result = client.get()
|
|
.uri("/persons/{id}", id).accept(MediaType.APPLICATION_JSON)
|
|
.retrieve()
|
|
.onStatus(HttpStatusCode::is4xxClientError, response -> ...)
|
|
.onStatus(HttpStatusCode::is5xxServerError, response -> ...)
|
|
.bodyToMono(Person.class);
|
|
----
|
|
|
|
Kotlin::
|
|
+
|
|
[source,kotlin,indent=0,subs="verbatim,quotes"]
|
|
----
|
|
val result = client.get()
|
|
.uri("/persons/{id}", id).accept(MediaType.APPLICATION_JSON)
|
|
.retrieve()
|
|
.onStatus(HttpStatusCode::is4xxClientError) { ... }
|
|
.onStatus(HttpStatusCode::is5xxServerError) { ... }
|
|
.awaitBody<Person>()
|
|
----
|
|
======
|
|
|
|
|
|
|
|
|