Java 为什么使用返回类型为Iterable的jax rs服务<;MyClass>;收集时工作<;MyClass>;不';T
假设我有一个简单的类Person,其属性为name和last name。还假设我有一个简单的rest服务,其中包含返回人员列表的get端点Java 为什么使用返回类型为Iterable的jax rs服务<;MyClass>;收集时工作<;MyClass>;不';T,java,collections,jersey,jax-rs,iterable,Java,Collections,Jersey,Jax Rs,Iterable,假设我有一个简单的类Person,其属性为name和last name。还假设我有一个简单的rest服务,其中包含返回人员列表的get端点 public class Person { // name and last name properties plus getters and setters } // Service @Stateless @LocalBean @Path("test") public class SimpleRestService { //Suppose
public class Person {
// name and last name properties plus getters and setters
}
// Service
@Stateless
@LocalBean
@Path("test")
public class SimpleRestService {
//Suppose people is initialized and contains actual people
private ArrayList<Person> people;
@GET
@PATH("/people/iterable")
public Iterable<Person> getPeople(){
//This one works
return people;
}
@GET
@PATH("/people/collection")
public Collection<Person> getPeople(){
//This one fails. HTTP 500. Nothing on server.log ...
return people;
}
}
公共类人物{
//名称和姓氏属性加上getter和setter
}
//服务
@无国籍
@本地豆
@路径(“测试”)
公共类SimpleRestService{
//假设人员已初始化并包含实际人员
私人ArrayList人;
@得到
@路径(“/people/iterable”)
公共IT人员(){
//这个有效
还人,;
}
@得到
@路径(“/people/collection”)
公共集合getPeople(){
//这个失败。HTTP 500。server.log上没有任何内容。。。
还人,;
}
}
第一个端点测试/people/iterable将正常工作,而测试/people/collection将在HTTP 500中失败。有人对此有线索吗 Jersey有一个注册表,将
@products
中的MIME类型与方法声明返回的Java类型以及处理对象以生成响应负载的处理程序相关联。它有一个将Iterable
和List
转换为JSON数组的处理程序。它还有一个将Map
转换为JSON对象的处理程序。Java接口集合
的问题在于,没有指示它应该表示为顺序集合(JSON数组
)还是命名项的无序集合(JSON对象
)。因此,默认情况下没有注册用于将集合
转换为应用程序/json
的处理程序。这是应用程序服务器上记录的异常中的消息所指示的。对此我不太确定。它必须更多地使用泛型和。我做了一些测试,创建了一个Person类和上面的服务,但是我能够无差别地返回Iterable或Collection。然后我添加了一些泛型,它对Iterable和Collection都不起作用。当我能够返回Iterable而不是Collection时,我试图重现完全相同的场景,但不幸的是,我不能这样做。然后。。。我不知道这是否仍然是一个有效的问题,因为我无法复制它