java – Jersey’NoContent’响应返回200而不是204

我正在使用Jersey(1.18)为我的WebApplication构建REST API.在我的部分代码中,我有以下代码段.
return Response.status(Status.NO_CONTENT).entity(err_message).build();

其中Status是com.sun.jersey.api.client.ClientResponse.Status的实例;

根据Jersey文档,NO_CONTENT应返回204代码,而不是这个,http响应有一个包含200个代码的标头.

NO_CONTENT
public static final ClientResponse.Status NO_CONTENT
204 No Content,see HTTP/1.1 documentation.

我试图将上述代码更改为

return Response.noContent().entity(err_message).build();

但问题仍然存在.
作为旁注,使用NOT_FOUND而不是NO_CONTENT,按预期返回404标头.

关于’如何返回204代码?’的任何建议,这是一个错误或我做错了什么.

注意:不是Returning 200 response code instead of 204的副本

解决方法

this SO answer说,

…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.

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().

换句话说,如果您想要“NO_CONTENT”,则不要在回复中包含内容.

相关文章

Java中的String是不可变对象 在面向对象及函数编程语言中,不...
String, StringBuffer 和 StringBuilder 可变性 String不可变...
序列化:把对象转换为字节序列的过程称为对象的序列化. 反序...
先说结论,是对象!可以继续往下看 数组是不是对象 什么是对...
为什么浮点数 float 或 double 运算的时候会有精度丢失的风险...
面试题引入 这里引申出一个经典问题,看下面代码 Integer a ...