Questions Management – Composite Option Service – Build API add new option using Spring WebFlux

Check out the full series of Questions Management tutorial here.

Before building the API add new option for the Composite Option Service, there are a few things we have to do first:

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

Question:

Option:

Since the Composite Option Service will call the Core Question Service and Core Option Service, we will add their information in the application.properties file as follows:

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 implementation is CoreOptionServiceImpl:

I will run this service using port 8183 so I will 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 CompositeOptionController with the following content:

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

Next I will create a CompositeOptionService to handle all operations related to the Composite Option Service:

The first operation is add a new option.

To implement this operation, based on the option information need added, we will call the Core Question Service to see the question id that this option belongs to does exist or not. If it is existing, we will call Core Option Service to perform add new this option.

Specifically, I will add an abstract method to the CompositeOptionService class as follows:

In order to implement the addNewOption() method above, we first need to call the Core Question Service to find the question base on the id of the question that the new Option object belongs to. By adding a new method in the CoreQuestionService:

with the implementation of the CoreQuestionServiceImpl class is as follows:

Next we will call to the Core Option Service to add a new option 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 addNewOption() method in our CompositeOptionService class.

We will inject CoreQuestionService and CoreOptionService service classes first:

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

If there is a question with the id that the object needs to add, the item will be returned. We will then call the Core Option Service to perform adding new option.

Calling the Core Option Service takes time to process, so I ran the process on another thread using the subscribeOn() method.

The last thing we need to do is to add a new method to the CompositeOptionController class to expose a POST request “/option/add”:

then declare the CompositeOptionService object with @Autowired annotation:

to be able to use the addNewOption() method of the Composite Option Service we created earlier:

OK, here we have completed the API add new option for Composite Option Service . Let’s test it.

Currently I have some questions as follows:

Questions Management – Composite Option Service – Build API add new option using Spring WebFlux

Suppose I now need to add a new option to the question with id 5b697df545aac307e5c68be5:

Questions Management – Composite Option Service – Build API add new option using Spring WebFlux

The result will be:

Questions Management – Composite Option Service – Build API add new option using Spring WebFlux

Add Comment