Questions Management – Core Category Service – Build API updating category using Spring WebFlux and Spring Data MongoDB Reactive

Check out the full series of Questions Management tutorial here.

As I mentioned earlier, in the previous tutorial, we have prepared all the necessary information and configurations to be able to build API updating category such as a document Category object containing information about a category, CategoryRepository to manipulate MongoDB, a CategoryController that defines the APIs of the Core Category Service will start with “/category” and the connection information to the MongoDB server is configured in the application.properties file. Now, we will proceed to build API to update a category.

To build API updating category, I would add a method to expose a PUT request “{id}” with the id being the category id that we need to update. We also need to define the body data, is the new content of the category that we will update as follows:

The steps to update a category include:

First, we need to check that the category we need to update does exist or not based on the id that the user is passing.

Spring Data MongoDB Reactive has provided us with a way to search by id so we just need to call it.

In case this category exists, we will use the information passed in the body data to update the information in the database:

Then returning to the user the new information of this category after updating the database with the HTTP status code of 200 OK.

In case this category does not exist in the database, we will return the result of HTTP status code is 404 Not Found.

The entire content of the updateCategory() method will now look like this:

Now that we’ve finished building the API to update a category for the Core Category Service, let’s try it out.

Suppose currently in the database I have the following categories:

Questions Management – Core Category Service – Build API updating category using Spring WebFlux and Spring Data MongoDB Reactive

then when I request to update the category with id is 5b297751366e2605cf3c0d3e:

Questions Management – Core Category Service – Build API updating category using Spring WebFlux and Spring Data MongoDB Reactive

The result will be:

Questions Management – Core Category Service – Build API updating category using Spring WebFlux and Spring Data MongoDB Reactive



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

In the previous tutorial, I created a new test class for CategoryController called CategoryControllerTest, a Mock object for CategoryRepository, and injected this Mock object into the CategoryController class:

Initialize mock every time I run a test case:

Now we will add two methods to test for the updateCategory() method of the CategoryController class as follows:

A method to test if there is a category based on passing id:

A method to test for the absence of category based on passing id:

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

Add Comment