Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
705 views
in Technique[技术] by (71.8m points)

rest - RESTful resource not found. 404 or 204? Jersey returns 204 on null being returned from handler

If you are looking for /Resource/Id and that resource does not exist, I had always though that 404 was the appropriate response. However, when returning null from a Jersey handler, I get back a "204 No Content". I can likely work with either one, but am curious to others thoughts on this.

To answer my own next question. To get Jersey to return 404 you must throw an exception.

    if (a == null)
        throw new WebApplicationException(404);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The HTTP Code Definition states that the codes beginning with 2 are for successful calls and 4 for unsuccessful ones.

  • When you get the 204 it just shows you that there is nothing to return (usually you expect if you make a POST or PUT call that does not need to return anything)

  • When you get a 404 it states that the call was unsuccessful, so it will not return anything.

In your situation the 404 is appropriate, not the 204! Since you probably want to say to the caller that he made a wrong call.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...