Check out the full series of Questions Management tutorial here.
In the previous tutorial, we prepared all the necessary configurations to be able to construct API delete option such as an object containing all the information of an Option; a CoreOptionService interface with the implementation CoreOptionServiceImpl is responsible for handling the Core Option Service; an OptionController defines the APIs for the API Option Service that will start with “/option” and information about the Core Option Service is configured in the application.properties file. Now, let’s build this API!
To build API delete option, I would first add a new deleteOption() method in the CoreOptionService interface:
1 |
Mono<HttpStatus> deleteOption(String optionId); |
The implementation of this method calls the Core Option Service in the CoreOptionServiceImpl class as follows:
1 2 3 4 5 6 7 8 9 10 11 |
@Override public Mono<HttpStatus> deleteOption(String optionId) { WebClient webClient = WebClient.builder() .baseUrl(getServiceUrl()) .build(); return webClient.delete() .uri("/option/" + optionId) .exchange() .map(response -> response.statusCode()); } |
As you can see, here I have used the WebClient object to connect to the Core Option Service and call the API delete option of this service with the URI “/option/{id}”.
Next, I’m going to add a method in OptionController that calls the deleteOption() method of CoreOptionService to expose a DELETE request “/option/{id}” as follows:
1 2 3 4 5 |
@DeleteMapping("/{id}") public Mono<ResponseEntity<Void>> deleteOption(@PathVariable(value = "id") String optionId) { return coreOptionService.deleteOption(optionId) .map(statusCode -> new ResponseEntity<Void>(statusCode)); } |
At this point, we have completed the API delete option for the API Option Service, let’s try it out.
Assuming the current in the database I have the following options:
so If I delete option with id 5b871766f62c5604f6a6a8fb, the result will be as follows: