DELETE

Handle a DELETE request with @DeleteMapping, taking the category's id from the URL, and answer 204 No Content.

Journeyman · Spring Boot · REST API · Updated

Deleting a record should be done through a DELETE request. When a client DELETEs a record - the id should be included in the URL

A DELETE request is a header only: the verb DELETE and the address of the category, /categories/10

The server will delete the data, then it will return a status of no content (204).

The server answers 204 (no content), with no body

DELETE Request - Client

In Insomnia, this is your Delete Snacks request from Insomnia: DELETE, with no body:

DELETE {{ _.base_url }}/api/categories/10

DELETE Request - Server

The method responsible for handling the request must be marked with a @DeleteMapping annotation to handle the request. The id of the resource must be marked as a @PathVariable. The @ResponseStatus annotation specifies the status code that will be returned (HttpStatus.NO_CONTENT returns a status of 204)

@DeleteMapping(path="/api/categories/{id}")
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void deleteCategory(@PathVariable int id)
{
    // the DAO deletes the row; nothing is returned
    categoryDao.delete(id);
}

DELETE Response - Client

Add the method to your controller and the delete method to your DAO, run the app again, and send Delete Snacks. The 405 Method Not Allowed from Insomnia: DELETE is now 204 No Content, and the body is empty. Then send All categories: category 10 is gone from the list.

Insomnia shows 204 No Content and an empty body after DELETE /api/categories/10

Cheat sheets