Is it correct to return 404 when a REST resource is not found?

前端 未结 3 814
无人共我
无人共我 2020-12-02 10:35

Let\'s say I have a simple (Jersey) REST resource as follows:

@Path(\"/foos\")
public class MyRestlet
        extends BaseRestlet
{

    @GET
    @Path(\"/{f         


        
3条回答
  •  自闭症患者
    2020-12-02 11:02

    Yes, it is pretty common to return 404 for a resource not being found. Just like a web page, when it's not found, you get a 404. It's not just REST, but an HTTP standard.

    Every resource should have a URL location. URLs don't need to be static, they can be templated. So it's possible for the actual requested URL to not have a resource. It is the server's duty to break down the URL from the template to look for the resource. If they resource doesn't exist, then it's "Not Found"

    Here's from the HTTP 1.1 spec

    404 Not Found

    The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable.


    Here's for 204

    204 No Content

    The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation. The response MAY include new or updated metainformation in the form of entity-headers, which if present SHOULD be associated with the requested variant.

    If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent. This response is primarily intended to allow input for actions to take place without causing a change to the user agent's active document view, although any new or updated metainformation SHOULD be applied to the document currently in the user agent's active view.

    The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.

    Normally 204 would be used when a representation has been updated or created and there's no need to send an response body back. In the case of a POST, you could send back just the Location of the newly created resource. Something like

    @POST
    @Path("/something")
    @Consumes(...)
    public Response createBuzz(Domain domain, @Context UriInfo uriInfo) {
        int domainId = // create domain and get created id
        UriBuilder builder = uriInfo.getAbsolutePathBuilder();
        builder.path(Integer.toString(domainId));  // concatenate the id.
        return Response.created(builder.build()).build();
    }
    

    The created(URI) will send back the response with the newly created URI in the Location header.


    Adding to the first part. You just need to keep in mind that every request from a client is a request to access a resource, whether it's just to GET it, or update with PUT. And a resource can be anything on the server. If the resource doesn't exist, then a general response would be to tell the client we can't find that resource.

    To expand on your example. Let's say FooService accsses the DB. Each row in the database can be considered a resource. And each of those rows (resources) has a unique URL, like foo/db/1 might locate a row with a primary key 1. If the id can't be found, then that resource is "Not Found"

提交回复
热议问题