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

Check out the full series of Questions Management tutorial here.

Before creating the API add new option for the API Option Service, there are a few things we need to do:

The first thing we need to do is add an Option object to contain the information of an option.

Since API Option Service will call Core Option Service and Composite Option Service, we will also configure information about these services in the application.properties file as follows:

To handle the call to Composite Option Service, I will create an interface named CompositeOptionService:

with the implementation of CompositeOptionServiceImpl:

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 8283 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!



I will create a new controller called ApiOptionController with the following content:

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

Next we will inject CompositeOptionService and CoreOptionService in to use:

To build the API add new option, I’m going to add a new method in the CompositeOptionService class named:

with the implementation of the CompositeOptionServiceImpl class is as follows:

As you can see, here I have used the WebClient object to connect to the Composite Option Service and call the API add a new option of this service with the “/option/add” URI.

Next, I will add a new method in ApiOptionController to expose a POST request “/option/add” to add a new option:

Our task is to simply call the CompositeOptionService addNewOption() method:

At this point, we have completed the API to add a new option for API Option Service. Let’s try it out!

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

Add Comment