Fetching data from 2 REST endpoints asynchronously and merging the responses
Fetching data from 2 REST endpoints asynchronously and merging the responses.
This Application gathers data from two REST endpoints asynchronously, merges the responses and displays them as a JSON.
As an example, you could use these two endpoints:
This project is built using the following tools:
The logic of the solution is very straightforward, the Application uses Spring’s @EnableAsync
to configure a ThreadPoolTaskExecutor
which will be used to run async methods.
In this application, fetching user and his data configured to run asynchronously using this ThreadPoolTaskExecutor
provided. Spring takes care of managing this executor.
As Maven is used as the build system, run:
./mvnw clean install
To run the project, just run one of the following commands:
java -jar target/data-1.0-SNAPSHOT.jar
./mvnw spring-boot:run
Navigate to http://localhost:8080/swagger-ui.html to see the available endpoints.
To run the tests, use:
./mvnw clean test
ThreadPoolTaskExecutor
in AsyncConfig
configurable from Spring Application properties or from environment variables.SpringBootTest
are expensive as it results in loading the entire Application Context, add more tests using WebMvcTest
.