我正在使用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标头.
解决方法
见
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 returnResponse.status(204).build()
.