在JAX RS中,返回Response和Bean或Bean集合(DTO)之间的区别 [英] In JAX RS, differences between returning Response and Bean or Collection of Beans (DTO)

查看:135
本文介绍了在JAX RS中,返回Response和Bean或Bean集合(DTO)之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建REST api.我的问题是,在使用Jersey时,我的服务构建与返回Response对象或返回Bean或集合之间的区别是什么?我只关心成功的通话,我会为错误和异常情况抛出适当的异常.

I am working on building a REST api. My question is, when using Jersey, what are the differences between my services building and returning a Response object or returning the the bean or collection. I am only concerned with successful calls, I am throwing appropriate exceptions for errors and exceptional situations.

这里是一个例子:

@Produces(MediaType.APPLICATION_JSON)
public Response search(FooBean foo){
    List<FooBean> results = bar.search(foo);
    return Response.ok(results).build();
}

vs.

@Produces(MediaType.APPLICATION_JSON)
public List<FooBean> search(FooBean foo){
    List<FooBean> results = bar.search(foo);
    return results;
}

我已经看过两个示例,并且我更喜欢第二种情况,只是为了使其更易于识别服务方法.我已经检查了对这两种方法的响应,它们似乎是相同的.

I've seen both examples used, and I'd prefer the second scenario, just to make it easier to recognize the service method. I've examined the responses to both of these methods and they appear to be identical.

有想法吗?

推荐答案

差异在JAX-RS规范中进行了解释:

The differences are explained in the JAX-RS specification:

3.3.3返回类型

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

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

无效
结果将显示一个空的实体,其状态代码为204.

回复
结果是从响应的实体属性映射到具有由响应的状态属性指定的状态码的实体主体.返回值为空将返回204状态码.如果未设置响应"的status属性,则将200状态代码用于非null实体属性,如果entity属性为null,则使用204状态代码.

GenericEntity
结果是从GenericEntity的Entity属性映射的实体主体.如果返回值不为null,则使用200状态代码,如果返回值为null,则返回204状态代码.

其他
结果是从返回的实例的类映射的实体主体.如果返回值不为null,则使用200状态代码,如果返回值为null,则返回204状态代码.

void
Results in an empty entity body with a 204 status code.

Response
Results 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.

GenericEntity
Results 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.

Other
Results 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.

需要为响应提供其他元数据的方法应返回Response的实例 ,ResponseBuilder类提供了一种使用构建器模式创建Response实例的便捷方法

Methods that need to provide additional metadata with a response should return an instance of Response, the ResponseBuilder class provides a convenient way to create a Response instance using a builder pattern.

'常规'bean的映射方式几乎与Response相同,不同之处在于Response允许您设置其他元数据(响应标头,特殊状态,特殊内容类型等).至于要使用哪一个,完全取决于您自己决定-Response给您带来更大的灵活性,但是常规的bean更具自我记录性".

'Regular' beans are mapped over in pretty much the same way as Response is, with the exception that a Response allows you to set additional metadata (response headers, specialized status, specialized content type, etc). As far as which one to use, thats entirely up to you too decide - Response gives you more flexibility, but regular beans are more 'self-documenting'.

这篇关于在JAX RS中,返回Response和Bean或Bean集合(DTO)之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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