Card 05/ 06
Practice: Writing a DELETE Endpoint That Returns 204
Remove a ride and answer with no body, because there is nothing left to describe.
A delete endpoint usually answers 204 No Content: the request succeeded, and the response has no body. Write a handler for DELETE /rides/{id} that removes the ride from the controller's rides map and returns 204, using the mapping annotations and ResponseEntity. Then decide what it should answer when the ride does not exist. A map's remove returns the value it removed, or null when there was none, which is how a handler can tell the two cases apart.
See a solution
@DeleteMapping("/{id}")
ResponseEntity<Void> deleteRide(@PathVariable String id) {
rides.remove(id);
return ResponseEntity.noContent().build();
}@DeleteMapping maps the verb, @PathVariable reads the id, and ResponseEntity<Void> says the response has no body. noContent() builds the 204.
For a ride that does not exist, both answers are common. Returning 204 anyway treats delete as "make sure it is gone", so repeating the request is harmless. Returning 404 tells the client its id was wrong. Either works if the whole API does the same.
← → move through sections and cards