在Java中,从1.5开始,我们就可以使用泛型了(generic),这看上去很像C++ Template,但是实际上它们是不同的。在这里我不想过多的描述细节,你可以从Google上搜索一下。 但是,泛型已经变得如此复杂,以至于已经有500多页的 FAQ。

我们长话短说:泛型提供了编译时类型安全,所以也消除了类型转换的(cast)的需要。它是通过被称为类型消除(type erasure)的编译时技术来实现的。 泛型FAQ解释了所有的细节,对我来说它就是Java泛型的圣经。
在有些情况下,我们需要从JAXRS的Response类的资源方法中饭后参数化的类型。 因为类型消除,使得此种情况下Jersey运行环境需要特殊的处理来决定为泛型类型选择相应的MessageBodyWriter。
对于使用Jersey的JAX-RS的开发者而言有很多可供选择的技术,下面我将详细的讨论其中的几个。
让我们看一个简单的域模型:Employee。

 package net.javasight;

 import javax.xml.bind.annotation.XmlRootElement;

 @XmlRootElement(name = "employee")
public class EmployeeBean {
private Long id;
private String firstName;
private String lastName; public EmployeeBean() {
// required for JAXB
} public EmployeeBean(Long id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
} public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getFirstName() {
return firstName;
} public void setFirstName(String firstName) {
this.firstName = firstName;
} public String getLastName() {
return lastName;
} public void setLastName(String lastName) {
this.lastName = lastName;
}
}

employee资源的一个实现如下:

 @Path("/employees")
public class EmployeeResource {
@GET
public Collection<EmployeeBean> getEmployees() {
EmployeeBean emp = new EmployeeBean(1L, "John", "Doe");
return Collections.singletonList(emp);
}
}

在这种情况下,我们从资源方法中返回了EmployeeBean的Collection集合。如果你从如下地址(http://localhost:9998/employees)访问该资源,将会产生如下的XML输出:

 <? xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>
<employeeBeans>
<employee>
<firstName>John</firstName>
<id>1</id>
<lastName>Doe</lastName>
</employee>
</employeeBeans>

我希望看到employee在<employees>标签中,而不是在<employeeBeans>中。这里需要做一些额外的配置来产生该格式。那么让我们修改下Employee的POJO来包含该集合。

 package net.javasight;

 import java.util.List;

 import javax.xml.bind.annotation.XmlRootElement;

 @XmlRootElement
public class Employees {
private List<EmployeeBean> employee; public Employees(List<EmployeeBean> employee) {
this.employee = employee;
} public Employees() {
// required for JAXB
} public List<EmployeeBean> getEmployee() {
return employee;
} public void setEmployee(List<EmployeeBean> employee) {
this.employee = employee;
}
}

让我们完成EmployeeResource的方法来使用自定义的POJO来产生相关的XML。

 package net.javasight;

 @Path("/employees")
public class EmployeeResource {
@GET
@Path("test1")
public Employees getEmployees1() {
EmployeeBean emp = new EmployeeBean(1L, "John", "Doe");
Employees employees = new Employees(Collections.singletonList(emp));
return employees;
} @GET
@Path("test2")
public Response getEmployees2() {
EmployeeBean emp = new EmployeeBean(1L, "John", "Doe");
Employees employees = new Employees(Collections.singletonList(emp));
return Response.ok(employees).build();
}
}

现在访问http://localhost:9988/employees/test1或http://localhost:9998/employees/test2应该会产生如下的XML:

 <? xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>
<employees>
<employee>
<firstName>John</firstName>
<id>1</id>
<lastName>Doe</lastName>
</employee>
</employees>

但是,难道我们需要这样糟糕的方法来产生该输出吗?已经不需要了,在Jersey1.2发布后有了一些改善。可以启用资源配置的FEATURE_XMLROOTELEMENT_PROCESSING 特性后,应当可以自动的产生该输出。因此,访问http://localhost:9998/employees/test1应该产生这种的格式XML。该属性默认是禁用的。
现在让我们看看在JAX-RS Response中返回参数化类型会遇到的实际的问题。我在EmployeeResource中添加了另一个方法。

@GET
@Path("test3")
@Produces(MediaType.APPLICATION_XML)
public Response getEmployees3() {
EmployeeBean emp = new EmployeeBean(1L, "John", "Doe");
List<EmployeeBean> list = new ArrayList<EmployeeBean>();
list.add(emp);
return Response.ok(list).build();
}

现在通过http://localhost:9998/employees/test3访问该方法应该会产生如下异常。我相信该异常在Jersey/JAX-RS用户中应该很常见。

 SEVERE: A message body writer for Java class java.util.ArrayList, and Java type class java.util.ArrayList, and MIME media type application/xml was not found
Jul 24 , 2010 11 : 58 : 55 PM com.sun.jersey.spi.container.ContainerResponse write
SEVERE: The registered message body writers compatible with the MIME media type are:
application/xml ->
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$App
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$App
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$App
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.server.impl.template.ViewableMessageBodyWriter
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
Jul 24 , 2010 11 : 58 : 55 PM com.sun.jersey.spi.container.ContainerResponse traceException
SEVERE: Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException

要修复该问题,我们需要告诉JAX-RS运行时响应实体的类型,在我们的情况下是Employee的集合。JAX-RS APIGenericEntity正是用于该目的。GenericEntity可以用于反映响应实体中的泛型类型。 EmployeeResource方法被更新在返回集合类型时使用GenericEntity。

@GET
@Path("test4")
@Produces(MediaType.APPLICATION_XML)
public Response getEmployees4() {
EmployeeBean emp = new EmployeeBean(1L, "John", "Doe");
List<EmployeeBean> list = new ArrayList<EmployeeBean>();
list.add(emp);
GenericEntity entity = new GenericEntity<List<EmployeeBean>>(list) {
};
return Response.ok(entity).build();
}

访问http://localhost:9998/employees/test4应该会产生我们所需要的输出。
除了这种方式,Jersey1.2引入了新的APIJResponse来支持这个,并且更优。JResponse是类型安全的Response,它保留了响应实体的类型信息,因此不需要额外的使用GenericEntity。
使用JResponse更新后的方法如下:

 @GET
@Path("test5")
@Produces(MediaType.APPLICATION_XML)
public JResponse<List<EmployeeBean>> getEmployees5() {
EmployeeBean emp = new EmployeeBean(1L, "John", "Doe");
List<EmployeeBean> list = new ArrayList<EmployeeBean>();
list.add(emp);
return JResponse.ok(list).build();
}

访问http://localhost:9998/employees/test5应该会产生我们所需要的输出.
这两种方式实现起来都很容易。主要的不同是GenericEntity是JAX-RS API然而JResponse是Jersey API,它可能不能在其他的JAX-RS实现中使用,因此不具备移植性。 如果你仅仅使用Jersey,那么JResponse是更好的选择,因为它是类型安全的并与Response兼容。
下面是完整的服务端代码:

 package net.javasight;

 import com.sun.grizzly.http.SelectorThread;
import com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory;
import com.sun.jersey.core.util.FeaturesAndProperties;
import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map; public class Main {
private static int getPort(int defaultPort) {
String port = System.getenv("JERSEY_HTTP_PORT");
if (null != port) {
try {
return Integer.parseInt(port);
} catch (NumberFormatException e) {
}
}
return defaultPort;
} private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost/").port(getPort(9998))
.build();
} public static final URI BASE_URI = getBaseURI(); protected static SelectorThread startServer() throws IOException {
final Map<String, String> initParams = new HashMap<String, String>();
initParams.put("com.sun.jersey.config.property.packages",
"com.employee.resources");
initParams.put(FeaturesAndProperties.FEATURE_XMLROOTELEMENT_PROCESSING,
"true");
System.out.println("Starting grizzly...");
SelectorThread threadSelector = GrizzlyWebContainerFactory.create(
BASE_URI, initParams);
return threadSelector;
} public static void main(String[] args) throws IOException {
SelectorThread threadSelector = startServer();
System.out
.println(String
.format(
"Jersey app started with WADL available at "
+ "%sapplication.wadl\nTry out %shelloworld\nHit enter to stop it...",
BASE_URI, BASE_URI));
System.in.read();
threadSelector.stopEndpoint();
}
}

以上内容转自http://javasight.net/2011/05/generified-collections-in-jersey/,感谢作者,但是作者只提供了服务端的泛型处理,并没有提供客户端如何使用泛型。
个人解决客户端接受泛型方法:

服务端代码:

@GET
@Path("test4")
@Produces(MediaType.APPLICATION_JSON)
public Response getEmployees4() {
EmployeeBean emp = new EmployeeBean(1L, "John", "Doe");
EmployeeBean emp1 = new EmployeeBean(2L, "zhangsan", "lisi");
List<EmployeeBean> list = new ArrayList<EmployeeBean>();
list.add(emp);
list.add(emp1);
GenericEntity<List<EmployeeBean>> entity = new GenericEntity<List<EmployeeBean>>(list) {
};
return Response.ok(entity).build();
}

客户端代码:

 GenericType<List<EmployeeBean>> gt = new GenericType<List<EmployeeBean>>(){};
Object obj = wr.path("/message/test4").get(gt);
System.out.println(obj);

加红字体是rest服务地址 wr是webResource对象。

【转载】在Jersey JAX-RS 处理泛型List等Collection的更多相关文章

  1. 【转载】 C#中常见的泛型集合类有哪些

    在C#语言编程过程中,List集合类是最常见的泛型集合类,其实除了List集合,还有其他一些常用的泛型集合类,如字典类型Dictionary泛型集合类.先进先出的队列类型Queue泛型集合类.后进先出 ...

  2. 转载.NET 4.0中的泛型的协变和逆变

    先做点准备工作,定义两个类:Animal类和其子类Dog类,一个泛型接口IMyInterface<T>, 他们的定义如下:   public class Animal { } public ...

  3. 转载:C#中的泛型

    泛型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性.泛型为.NET框架引入了类型参数(type parameters)的概念.类型参数使得设计类和方法时,不必确定一个或多个具 ...

  4. C# 泛型集合

    原文出处我的wiki,转载请说明出处 考虑到泛型在集合类型中的广泛应用,这里一起讨论. 1. 泛型 1.1 泛型的定义与约束 创建泛型方法.委托.接口或类时,需要在名称后增加尖括号及其中的泛型参数,泛 ...

  5. 【转载】C#中List集合使用AddRange方法将一个集合加入到指定集合末尾

    C#编程开发过程中,List集合是时常使用到的集合对象,如果在List集合的操作中需要将1个List集合加入到另一个List集合的末尾,则可以使用List集合的AddRange方法来实现,AddRan ...

  6. Java泛型

    什么是泛型? 泛型(Generic type 或者 generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类.可以把类型参数看作是使用参数化类型时指定的类型的一个 ...

  7. C#中的泛型 【转】

    C#中的泛型 泛型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性.泛型为.NET框架引入了类型参数(type parameters)的概念.类型参数使得设计类和方法时,不必确 ...

  8. Java泛型总结

    1. 什么是泛型?泛型(Generic type 或者 generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类.可以把类型参数看作是使用参数化类型时指定的类型的 ...

  9. java泛型的讲解

    java泛型 什么是泛型? 泛型(Generic type 或者 generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类.可以把类型参数看作是使用参数化类型时指 ...

随机推荐

  1. Windows自带的端口转发工具netsh使用方法

    微软Windows的netsh是一个命令行脚本实用工具.使用netsh工具 ,可以查看或更改本地计算机或远程计算机的网络配置.不仅可以在本地计算机上运行这些命令,而且可以在网络上的远程计算机上运行. ...

  2. C# 个人疏漏整理

    1.dynamic和var不能混为一谈. var声明局部变量只是一种简化语法,var要求编译器根据之后的表达式推断具体的数据类型. var只能用于声明方法内部的局部变量,dynamic则可用于局部变量 ...

  3. kubernetes和calico集成

    硬件环境: 三台虚拟机: 192.168.99.129 master(kube-apiserver.kube-controller-manager.kube-proxy.kube-scheduler. ...

  4. php 路由实现

    因为有小的业务逻辑比较复杂,orm用起来很麻烦,但是多入口又不好控制,每个页面都去包含,多了就迷了,所以就只写了个路由,加了个防注入 封装增删改查好无聊,直接封装业务逻辑,为业务而开发,业务逻辑里是面 ...

  5. CI和CD的意思

    openstack中CI和CD的意思: 持续集成(CI)和持续交付(CD)

  6. 你不知道的Console命令

    一.显示信息的命令 1: <!DOCTYPE html> 2: <html> 3: <head> 4: <title>常用console命令</t ...

  7. iOS 开发之--打测试包的时候报错的解决方法

    在打测试包的时候,一直出现一个报错,解决方法如下: 方法一: 描述文件所在目录是:~/Library/MobileDevice/Provisioning\ Profiles/,可以直接前往文件夹,把所 ...

  8. Bouncy Castle Crypto API c# port

    Bouncy Castle 是一种用于 Java 平台的开放源码的轻量级密码术包.它支持大量的密码术算法,并提供 JCE 1.2.1 的实现.现在有了C#的版本.下面是网站上的介绍 This port ...

  9. Android实例-多窗口的切换

    Android实例-多窗口的切换 1.图片一是程序运行后的界面. 2.图片二是点击"非模态显示"的界面. 3.图片三是点击"模诚显示"的界面(提示平台不支持). ...

  10. location.assign 与 location.replace的区别

    window.location.assign(url) : 加载 URL 指定的新的 HTML 文档. 就相当于一个链接,跳转到指定的url,当前页面会转为新页面内容,可以点击后退返回上一个页面. w ...