我正在尝试 RESTEasy 网络服务。我编写了简单的服务来返回 JAXB 客户对象列表,并期望返回的 xml 是 Collection 标签下的客户标签的集合。但我得到的是<Collection/>
,意味着一个空集合。
我的代码是:
客户服务
@Path("/customers")
public class CustomerService {
List<Customer> customersList = new ArrayList<Customer>();
public CustomerService() {
customersList.add(new Customer(.....)); //Creating Customers using parametarized Cunstructor
customersList.add(new Customer(.....));
customersList.add(new Customer(.....));
customersList.add(new Customer(.....));
customersList.add(new Customer(.....));
}
@GET
@Produces("application/xml")
@Path("/xml/list")
public List<Customer> getAllCustomersList(){
return customersList; //nonEmpty list of Customers
}
}
客户(JAXB 对象)
@XmlRootElement
public class Customer {
private String customerId;
private String name;
private String address;
private int age;
public Customer() { }
public Customer(String customerId, String name, String address, int age) {
super();
this.customerId = customerId;
this.name = name;
this.address = address;
this.age = age;
}
@XmlAttribute
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@XmlElement
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
我可以通过这项服务获得单个客户,效果很好:
@GET
@Produces("application/xml")
@Path("/xml/{id}")
public Customer getCustomer(@PathParam("id") String customerId){
for(Customer customer: customersList){
if(customerId.equals(customer.getCustomerId()))
return customer;
}
return null;
}
我已经将服务设为单例,所以列表不为空,这是肯定的。甚至我已经调试了代码以确认列表不为空。我也尝试过使用数组,同样的情况正在发生。
这是事情,我正在阅读: http ://docs.jboss.org/resteasy/docs/2.3.0.GA/userguide/html/Built_in_JAXB_providers.html#JAXB_Collections
我在用
- WebSphere 应用服务器 8.0
- RESTEasy 2.2.3GA