如何在RESTful Web服务中使用jersey框架抛出HTTP 204状态代码? [英] how to throw HTTP 204 status code using jersey framework in RESTful web service?

查看:139
本文介绍了如何在RESTful Web服务中使用jersey框架抛出HTTP 204状态代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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 做的那样。其他状态代码正在休息客户端工具上正常显示,但何时显示 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表示没有内容,这意味着响应中不包含实体,但你将其中一个放入它。泽西岛很可能会把它换成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状态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆