java 如何在 RESTful Web 服务中使用 jersey 框架抛出 HTTP 204 状态代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14102797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:05:05  来源:igfitidea点击:

how to throw HTTP 204 status code using jersey framework in RESTful web service?

javaweb-servicesrestjerseyjax-rs

提问by Ashmi Suranse

I am using jersey framework to develop RESTful web service. I am throwing various HTTP status codes with response using following code:

我正在使用 jersey 框架来开发 RESTful Web 服务。我使用以下代码抛出各种带有响应的 HTTP 状态代码:

public class RestNoContentException extends WebApplicationException 
{
    public RestNoContentException(String message) 
    {
        super(Response.status(Status.NO_CONTENT)
            .entity(message).type("text/plain")
            .build());
        }
}

While testing the REST web service using Firefox Mozilla rest client tool, it is displaying 200 OKstatus 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 204status code, it is showing 200 OKstatus code.

使用 Firefox Mozilla rest 客户端工具测试 REST Web 服务时,它显示200 OK状态而不是204 NO CONTENT. 我正在以与处理状态代码相同的方式处理其他状态代码204。其他状态代码在rest客户端工具上正确显示204,但是当显示状态代码时,它显示200 OK状态代码。

Can someone please help me out here? what am I missing?

有人可以帮我吗?我错过了什么?

回答by Ryan Stewart

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 属于响应代码的“成功”类别,因此将其作为异常结果返回是一件非常非常奇怪的事情。

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 的意思是“No Content”,意思是响应不包含实体,但你在里面放了一个。Jersey 很可能会为您将其切换为 200,这与 204 基本相同,只是它包含一个响应实体。

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

最后,您可以通过几个内置行为非常简单地获得 204 响应:void 方法和 null 返回值都映射到 204 响应。否则,只需返回Response.status(204).build()

回答by secondflying

You shouldn't give entity if you want throw 204:

如果你想抛出 204,你不应该给实体:

@GET
@Produces(MediaType.TEXT_PLAIN)
public Response test() {
    //return Response.status(Status.NO_CONTENT).entity("hello").build(); //this will throw 200
    return Response.status(Status.NO_CONTENT).build();
}

回答by Bogdan

Just one thing to add to the already existing responses. What Jersey is doing is the correct behavior as for the spec:

只需要将一件事添加到现有的响应中即可。Jersey 正在做的是规范正确行为

3.3.3 Return Type

Resource methods MAY return void, Response, GenericEntity, or another Java type, these return types are mapped to a response entity body as follows:

voidResults in an empty entity body with a 204 status code.

ResponseResults in an entity body mapped from the entity property of the Response with the status code specified by the status property of the Response. A null return value results in a 204 status code. If the status property of the Response is not set: a 200 status code is used for a non-null entity property and a 204 status code is used if the entity property is null.

GenericEntityResults in an entity body mapped from the Entity property of the GenericEntity. If the return value is not null a 200 status code is used, a null return value results in a 204 status code.

OtherResults in an entity body mapped from the class of the returned instance. If the return value is not null a 200 status code is used, a null return value results in a 204 status code.

[...]

3.3.3 返回类型

资源方法可以返回 void、Response、GenericEntity 或其他 Java 类型,这些返回类型映射到响应实体主体如下:

void 导致带有 204 状态代码的空实体主体。

Response结果是从 Response 的实体属性映射的实体主体,其状态代码由 Response 的 status 属性指定。空返回值导致 204 状态代码。如果未设置 Response 的状态属性: 200 状态代码用于非空实体属性,如果实体属性为空,则使用 204 状态代码。

GenericEntity 生成从 GenericEntity 的 Entity 属性映射的实体主体。如果返回值不为空,则使用 200 状态代码,空返回值导致 204 状态代码。

从返回的实例的类映射的实体主体中的其他结果。如果返回值不为空,则使用 200 状态代码,空返回值导致 204 状态代码。

[...]

And since you are using an exception, the following section applies (emphasis mine):

并且由于您使用的是异常,因此以下部分适用(强调我的):

3.3.4 Exceptions

A resource method, sub-resource method or sub-resource locator may throw any checked or unchecked exception. An implementation MUST catch all exceptions and process them as follows:

  1. Instances of WebApplicationException MUST be mapped to a response as follows. If the response property of the exception does not contain an entity and an exception mapping provider (see section 4.4) is available for WebApplicationException an implementation MUST use the provider to create a new Response instance, otherwise the response property is used directly. The resulting Response instance is then processed according to section 3.3.3.

[...]

3.3.4 例外

资源方法、子资源方法或子资源定位器可能会抛出任何已检查或未检查的异常。实现必须捕获所有异常并按如下方式处理它们:

  1. WebApplicationException 的实例必须按如下方式映射到响应。如果异常的响应属性不包含实体并且异常映射提供者(参见第 4.4 节)可用于 WebApplicationException,则实现必须使用提供者创建新的响应实例,否则直接使用响应属性。然后根据第 3.3.3 节处理生成的 Response 实例。

[...]

So you should either return null, void or build a 204 response. You throw exceptions only if it's an exceptional case in your application and throwing the exception makes this clear.

因此,您应该返回 null、void 或构建 204 响应。仅当它是应用程序中的例外情况时才抛出异常,并且抛出异常可以清楚地表明这一点