Skip to content

Instantly share code, notes, and snippets.

@rvashishth
Last active October 11, 2020 11:24
Show Gist options
  • Save rvashishth/12f16bd57be93459c8f666cf305a11c8 to your computer and use it in GitHub Desktop.
Save rvashishth/12f16bd57be93459c8f666cf305a11c8 to your computer and use it in GitHub Desktop.
Spring Reactive WebClient Get Single Response Object
import groovy.transform.CompileStatic
import groovy.transform.ToString
import org.springframework.http.HttpMethod
import org.springframework.web.reactive.function.client.WebClient
import reactor.core.publisher.Mono
@CompileStatic
class TestMe {
static void main(String[] args) {
// Prepare webClient instance
WebClient webClient = WebClient.builder().baseUrl('https://jsonplaceholder.typicode.com').build()
// use retrieve - it's shortcut to response
Mono<User> userMono = webClient.method(HttpMethod.GET).uri('/todos/{id}',1).retrieve().bodyToMono(User.class)
// subscribe to userMono, and wait for result in blocking fashion. Returns user, null or an error
User user = userMono.block()
// prints output
println user
}
}
@ToString
class User {
String userId
String id
String title
String completed
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment