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

Check out the full series of Questions Management tutorial here.

The API Question Service will call other Composites and Core Services to process the user request and return results based on the type of data the user wants. So before building API to get all the questions for the API Question Service, there are a few things we need to do:

The first thing we need to do is to add a CompositeQuestion object to contain the question’s content.

Of course, we must also define other objects to contain the information of the question, category, and option as follows:

Question:

Category:

Option:

CompositeQuestion:

Since the API Question Service will call other Core and Composite Services, we will also configure the services in the application.properties file as follows:

To handle the call to the Composite Question Service, we will create an interface called CompositeQuestionService:

with the implementation of CompositeQuestionServiceImpl:

As you can see, in the CompositeQuestionServiceImpl class, I’ve injected information about the Composite Question Service’s base URL.

I will run this service using port 8182 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 controller called ApiQuestionController with the following content:

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

Next we will inject CompositeQuestionService to use:

To build API to get all the questions, I’m going to add a method in the CompositeQuestionService class named:

with implementation as follows:

As you can see, here I have used the WebClient object to connect to the Composite Question Service and call the API to get all questions with the “/question/all” URI.

Next, I will add a new method in ApiQuestionController to expose a GET request “/all” to get all the questions:

Our task is to simply call the CompositeQuestionService’s findAllQuestions() method:

At this point, we have completed the API to get all questions for the API Question Service. Let’s try it out!

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

Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment