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的副本
解决方法
…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 returnResponse.status(204).build()
.
换句话说,如果您想要“NO_CONTENT”,则不要在回复中包含内容.