Consuming REST APIs Using Reactor With Java Spring and WebClient
In Spring 5, a reactive web client was introduced that access REST API’s in a reactive, non-blocking way. An improvement over the much used RestTemplate. This can be also closed compared to RxJs or Angular’s HttpClient
to access REST API’s.
TL;DR
This article discusses about using Reactive programming to access API’s within Spring framework.
Build
- Build a new project using https://start.spring.io/ . Used
Spring Web
andSpring Reactive Web
as dependencies
2. Unzip the downloaded package, and run $mvn spring-boot:run
command at the root folder to make sure the default project builds and runs without any errors.
3. We will use the sample REST API from https://jsonplaceholder.typicode.com/
A call to https://jsonplaceholder.typicode.com/todos/1 shows the following result:
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
4. Let’s add a POJO, Controller and a WebClient