Search code examples
javaspring-bootspring-mvc

How to send Delete Response on Delete Mapping


error

How I fix it and perform both the operation . If I perform separately , first delete and then return noContent() then it's work but Is there any trick to Check If the operation is performed or not If not then return NOTFOUND.


Solution

  • To check if the deletion operation is successful or not and return NOT_FOUND if it fails, you can modify your code by checking the result of the deletion operation before returning the response.

    @DeleteMapping("/{hotelId}")
    public ResponseEntity<?> deleteHotel(@PathVariable String hotelId) {
        boolean deletionSuccessful = hotelService.delete(hotelId);
    
        if (deletionSuccessful) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }