问题描述
我正在使用 jersey 框架来开发 RESTful Web 服务.我使用以下代码响应各种 HTTP 状态代码:
I am using jersey framework to develop RESTful web service. I am throwing various HTTP status codes with response using following code:
public class RestNoContentException extends WebApplicationException
{
public RestNoContentException(String message)
{
super(Response.status(Status.NO_CONTENT)
.entity(message).type("text/plain")
.build());
}
}
使用 Firefox Mozilla REST 客户端工具测试 REST Web 服务时,它显示的是 200 OK
状态,而不是 204 NO CONTENT
.我处理其他状态代码的方式与处理状态代码 204
的方式相同.其他状态码在 rest 客户端工具上正常显示,但是当显示 204
状态码时,它显示 200 OK
状态码.
While testing the REST web service using Firefox Mozilla rest client tool, it is displaying 200 OK
status instead of 204 NO CONTENT
. I am handling the other status codes the same way I am doing for status code 204
. Other status codes are appearing properly on rest client tool but when to show 204
status code, it is showing 200 OK
status code.
有人可以帮我吗?我错过了什么?
Can someone please help me out here? what am I missing?
推荐答案
首先,204 属于响应代码的成功"类别,因此将其作为异常结果返回是一件非常非常奇怪的事情.
First, 204 is in the "Successful" category of response codes, so returning it as the result of an exception is a very, very weird thing to do.
其次,204 表示No Content",表示响应中不包含实体,但您在其中放入了一个实体.Jersey 很可能会为您将其切换为 200,这与 204 基本相同,只是它包含一个响应实体.
Second, 204 means "No Content", meaning that the response contains no entity, but you put one in it. It's likely that Jersey is switching it to a 200 for you, which is basically identical to a 204 except that it contains a response entity.
最后,您可以通过几个内置行为非常简单地获得 204 响应:void 方法和 null 返回值都映射到 204 响应.否则,只需返回 Response.status(204).build()
.
Finally, you can get 204 responses very simply by a couple of built-in behaviors: void methods and null return values both map to a 204 response. Otherwise, simply return Response.status(204).build()
.
这篇关于如何在 RESTful Web 服务中使用 jersey 框架抛出 HTTP 204 状态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!