Questions Management – API Category Service – Build API retrieving all categories using Spring WebFlux

In the previous tutorial, we prepared all the necessary configurations to build API retrieving all categories from the Core Category Service in API Category Service as: a Category object to contain information about a category, a CoreCategoryService interface, with implementation is the CoreCategoryServiceImpl, is responsible for handling the Core Category Service, a CategoryController that defines the APIs for API Category Service that will start with “/category” and information about the Core Category Service configured in the application.properties file. Now, let’s build this API!

To build the API  retrieving all the categories, I would first add a findAll() method in the CoreCategoryService interface:

The implementation of this method in the CoreCategoryServiceImpl class is as follows:

As you can see, here I have used the WebClient object to connect to the Core Category Service and call the API retrieving all categories of this service with the “/category/all” URI.

Next, I’m going to add a method to expose a GET request “/all” in CategoryController:

Our task is to just call the CoreCategoryService’s findAll() method:

Now that we have finished building the API retrieving all the categories for the API Service Category, let’s test it.

Questions Management – API Category Service – Build API retrieving all categories using Spring WebFlux


Now we will add a new Unit Test for the code that we just added.

In the previous tutorial, I created a new class test for CategoryController named CategoryControllerTest, a Mock object for CoreCategoryService, and injected this Mock object into the CategoryController class, initializing the mock every time I run a test case:

Now I’m going to add a method to test the findAllCategories() method of the CategoryController class, like this:

Run “Maven test” in STS or “mvn test” with Apache Maven, you will not see any errors.

Add Comment