Questions Management – Core Option Service – Build API delete option using Spring WebFlux and Spring Data MongoDB Reactive

Check out the full series of Questions Management tutorial here.

In the previous tutorial, we prepared all the necessary configurations to be able to build API deletion option such as an Option document object containing the information of an option, an OptionRepository to manipulate MongoDB, an OptionController defines APIs for Core Option Service will start with “/option” and the connection information to the MongoDB server is configured in the application.properties file. Now, we are going to build this API!

To build API deletion option, I would add a method in the OptionController class to expose a DELETE request “{id}” with id as the id of the option that we need to delete:

The steps to delete an option include:

First, we need to check that the option we want to delete does exist or not based on the id that the user passing on.

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

In case this option exists, we will delete it and return the HTTP status code to 200 OK.

If it does not exist, return the HTTP status code of 404 Not Found.

The entire contents of the deleteOption() method will now look like this:

At this point, we have completed the API delete option for Core Option Service, let’s test it out!

Suppose I currently have the following options:

Questions Management – Core Option Service – Build API delete option using Spring WebFlux and Spring Data MongoDB Reactive

then when you request to delete option with id is 5ae874c345286708520dd0e5, the result will be:

Questions Management – Core Option Service – Build API delete option using Spring WebFlux and Spring Data MongoDB Reactive

Add Comment