Questions Management – Composite Question Service – Build API to get all the questions using Spring WebFlux

Check out the full series of Questions Management tutorial here.

Composite Service will connect to the Core Services to retrieve data, combine or transform those data according to the needs of the user and return them. So, before you build the API to get all the questions for the Composite Question Service, there are a few things we have to do first:

Because the Composite Question Service will deal with categories, questions, and options, we will add model objects for each type to contain their information as follows:

Category:

Question:

Option:

Here, we will have an object containing all the information about a question, the category that this question belongs to and its options, so we will also add a new CompositeQuestion object like this:

Because the Composite Question Service will call to the Core Category Service for the category information, to the Core Question Service for the question information, to the Core Option Service for the option, so I will add their information in the application.properties file as follows:

To handle the call to the Core Category Service, I will create an interface called CoreCategoryService:

with the implementation is CoreCategoryServiceImpl:

To handle the call to the Core Question Service, we will create an interface called CoreQuestionService:

with the implementation of CoreQuestionServiceImpl:

To handle the call to the Core Option Service, I will create an interface called CoreOptionService:

with the implementation is CoreOptionServiceImpl:

I will run this service using port 8181 so I will also add the server.port property in the application.properties file as follows:

OK, everything is ready, now I will go to the main part of this tutorial!



First, I will create a controller named CompositeQuestionController with the following content:

With this declaration, I also expose APIs for the Composite Question Service with the request URL starting with “/question”.

Next I will create a CompositeQuestionService to handle all the operations related to the Composite Question Service.

The first action will be to find and combine information of all the questions that are in the system.

This information includes all questions, the category that the questions belong to, and the options for that questions. We will use the CompositeQuestion object we have declared above to contain all of this information. Specifically, I will add an abstract method to the CompositeQuestionService class as follows:

With the findAllQuestions() method, we first need to call the Core Question Service to get all the questions that are in the system. Because the information from the Core Question Service only has a Category ID with the content of the question, so we need to call the Core Category Service to find the category information by ID and call to the Core Option Service to get all the options of that question by Question ID. That’s all we need to do for the findAllQuestions() method.

In order to implement the findAllQuestions() method, we first need to call the Core Question Service to get all the questions that are in the system by adding a new method in the CoreQuestionService:

with the implementation of the CoreQuestionServiceImpl class is as follows:

Next we will call the Core Category Service to find the category information by ID by adding a new method in the CoreCategoryService class:

with the implementation of the CoreCategoryServiceImpl class is as follows:

Finally, we will call the Core Option Service to get all options of this question by Question ID by adding a new method in the CoreOptionService class:

with the implementation of the CoreOptionServiceImpl class is as follows:

OK, now we will implement the findAllQuestions() method.

We will inject 3 core service classes CoreCategoryService, CoreQuestionService, and CoreOptionService first:

In the findAllQuestions() method, we will first call the Core Question Service:

For each item that the Core Question Service returns with this method, we will use that item to search for category information and options using the flatMap() method:

Here, we use the collectList() method to return a list of options, map() method to build a Composite object that we need. Another thing to keep in mind is that since calling the Core Category Service and the Core Option Service takes time to process, we have run this process on another thread using the subscribeOn() method.

The last thing we need to do is to add a new method to the CompositeQuestionController class to expose a GET request “/all”:

then declare autowire for the CompositeQuestionService object:

to be able to use the findAllQuestions() method we created above:

OK, here we have finished building the API to get all the questions for the Composite Question Service. Let’s test it.

Questions Management – Composite Question Service – Build API to get all the questions using Spring WebFlux

Add Comment