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

前端之家收集整理的这篇文章主要介绍了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@H_301_10@ public static final ClientResponse.Status NO_CONTENT@H_301_10@ 204 No Content,see HTTP/1.1 documentation.

我试图将上述代码更改为

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

但问题仍然存在.@H_301_10@作为旁注,使用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@H_301_10@ entity,but you put one in it. It’s likely that Jersey is switching it@H_301_10@ to a 200 for you,which is basically identical to a 204 except that it@H_301_10@ contains a response entity.

Finally,you can get 204 responses very simply by a couple of built-in@H_301_10@ behaviors: void methods and null return values both map to a 204@H_301_10@ response. Otherwise,simply return Response.status(204).build().

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

猜你在找的Java相关文章