2

I have a below method in my rest controller class

@DeleteMapping("/delete/{id}")
public ResponseEntity<?> deleteMovieById(@PathVariable Integer id) {
    try {
        service.deleteMovieById(id);
        responseEntity = new ResponseEntity<String>("Movie Deleted", HttpStatus.OK);
    } catch (MovieNotFoundException e) {
        **//Confused over here**
    } catch (Exception e) {
        responseEntity = new ResponseEntity<String>("Unable delete movie", HttpStatus.INTERNAL_SERVER_ERROR);
    }
    return responseEntity;
}

I have confused lot what should be response code if Movie Object Not Found while delete a movie

  1. NOT FOUND (404): usually we use it for URI/URL not found but here URL/URI is correct only requesting my content(movie id) only not in database.
  2. I see NOT FOUND(404) used in many examples even for no content matching.
  3. In this 404 is the right option then when to use 204(No Content)?

So anyone give me clear picture about these

3 Answers 3

4

I think 404 is definitely the right approach. The client is trying to do something to a specific resource. In this case DELETE it. When the resource cannot be found and therefore the delete can not be executed the only clear response to me seems a 404.

After all 404 is a client error that specifies that the resource the client is referring too cannot be found, but otherwise the request itself is valid.

204 means the server succesfully processed the request but returned no content. However the resource was not succesfully deleted because it was never found. So 204 is not applicable.

Sign up to request clarification or add additional context in comments.

Comments

3

I have confused lot what should be response code if Movie Object Not Found while delete a movie

If we think there is spelling error in the target-uri of the request, then I think 404 Not Found is appropriate, or 405 Method Not Allowed if the target uri identifies a resource where the delete operation is not supported.

If the spelling of the target-uri is correct, then things get more interesting.

A request method is considered "idempotent" if the intended effect on the server of multiple identical requests with that method is the same as the effect for a single such request. Of the request methods defined by this specification, PUT, DELETE, and safe request methods are idempotent.

Idempotent, very coarsely, means that a compliant server is limited to doing reasonable things when multiple copies of the same message are delivered. This shouldn't be a surprise; deleting something twice is the same as deleting something once.

Because unsafe request methods (Section 4.2.1 of [RFC7231]) such as PUT, POST or DELETE have the potential for changing state on the origin server, intervening caches can use them to keep their contents up to date.

A cache MUST invalidate the effective Request URI (Section 5.5 of [RFC7230]) as well as the URI(s) in the Location and Content-Location response header fields (if present) when a non-error status code is received in response to an unsafe request method.

And this is why we care -- if we play by the rules, then we support the standard cache semantics "for free"; clients can use any off the shelf cache, rather than requiring bespoke code.

An origin server MUST NOT perform the requested method if a received If-Match condition evaluates to false; instead, the origin server MUST respond with either a) the 412 (Precondition Failed) status code or b) one of the 2xx (Successful) status codes if the origin server has verified that a state change is being requested and the final state is already reflected in the current state of the target resource (i.e., the change requested by the user agent has already succeeded, but the user agent might not be aware of it, perhaps because the prior response was lost or a compatible change was made by some other user agent).

So in the case of a conditional DELETE, we're clearly permitted to send a 2xx response if the resource has already been deleted.

... and I have not found a counter argument in the specification to suggest that the same behavior should not be used for an unconditional delete. It's still idempotent, the current state of the resource reflects the intended final state, caches will do the right thing.

So sending back 200 Yup we deleted the movie or 204 No Content both look to me to be fine options. (Note: 204 doesn't mean that the resource has no content; it means that the HTTP Response has a message body that is 0 bytes long).

Whether or not it matters probably depends on how cache implementors have been interpreting RFC 7231 4.3.5 -- should caches invalidate their stored representations when a error status code is received?

But since we can be sure that at RFC 7234 compliant cache will do the right thing given non-error status codes, I would prefer to use them for this case.

1 Comment

I love the elaborate answer! I have one question though, would you differentiate between a 204 for a resource that is not found because it was already deleted, and 404 for a resource that is simply not there?
1

404(not found) is the expected behavior in these circumstances, because Movie not found (for delete). The good idea is to add proper error message for the client:

@DeleteMapping(path = "/users/{id}")
public void deleteUsers(@PathVariable Long id) {
    boolean success = service.delete(id);

    if(!success)
        throw new UserNotFoundException("User id = " + id);

}


@ResponseStatus(HttpStatus.NOT_FOUND)
public class UserNotFoundException extends RuntimeException {

    public UserNotFoundException(String message) {
        super(message);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.