RESTEasy服务,用于生成xml并返回给出空集合的JAXB对象的数组/列表

RESTEasy服务,用于生成xml并返回给出空集合的JAXB对象的数组/列表,xml,jaxb,jax-rs,resteasy,Xml,Jaxb,Jax Rs,Resteasy,我正在尝试RESTEasy Web服务。我编写了一个简单的服务来返回JAXB客户对象列表,并希望返回的xml是集合标记下的客户标记集合。但我得到的是,意思是一个空集合 我的代码是: 客户服务 @Path("/customers") public class CustomerService { List<Customer> customersList = new ArrayList<Customer>(); public CustomerService() {

我正在尝试RESTEasy Web服务。我编写了一个简单的服务来返回JAXB客户对象列表,并希望返回的xml是集合标记下的客户标记集合。但我得到的是
,意思是一个空集合

我的代码是:

客户服务

@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
}
}
我可以通过这项服务获得单一客户,效果很好:

@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;

    }
我已经将服务设置为singleton,因此列表不是空的,这是肯定的。甚至我也调试了代码以确认列表不是空的。 我也尝试了阵列,同样的情况也在发生

这是我读到的东西:

我正在使用

  • WebSphereApplicationServer8.0
  • RESTEasy 2.2.3GA

我可以通过以下更改使序列化与您的客户POJO一起工作:

  • @xmlement
    注释移动到字段中
  • 添加了
    @xmlacessortype(xmlacesstype.FIELD)
    注释,告知JAXB按字段绑定并忽略getter/setter
方法:

@GET
@Path("/customers")
@Produces(MediaType.APPLICATION_XML)
public List<Customer> getCustomer()
{
    List<Customer> customers = new ArrayList<Customer>();

    customers.add(new Customer("1", "customer1", "some address 1", 20));
    customers.add(new Customer("2", "customer2", "some address 2", 45));
    customers.add(new Customer("3", "customer3", "some address 3", 36));

    return customers;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Customer 
{
    @XmlAttribute
    private String customerId;

    @XmlElement
    private String name;

    @XmlElement
    private String address;

    @XmlElement
    private int age;

    public Customer()
    {

    }

    public Customer(String customerId, String name, String address, int age)
    {
        this.customerId = customerId;
        this.name = name;
        this.address = address;
        this.age = age;
    }

    public String getCustomerId() 
    {
        return customerId;
    }

    public void setCustomerId(String customerId) 
    {
        this.customerId = customerId;
    }

    public String getName() 
    {
        return name;
    }

    public void setName(String name) 
    {
        this.name = name;
    }

    public String getAddress()
    {
        return address;
    }

    public void setAddress(String address)
    {
        this.address = address;
    }

    public int getAge() 
    {
        return age;
    }

    public void setAge(int age) 
    {
        this.age = age;
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<collection>
    <customer customerId="1">
        <name>customer1</name>
        <address>some address 1</address>
        <age>20</age>
    </customer>
    <customer customerId="2">
        <name>customer2</name>
        <address>some address 2</address>
        <age>45</age>
    </customer>
    <customer customerId="3">
        <name>customer3</name>
        <address>some address 3</address>
        <age>36</age>
    </customer>
</collection>
输出XML:

@GET
@Path("/customers")
@Produces(MediaType.APPLICATION_XML)
public List<Customer> getCustomer()
{
    List<Customer> customers = new ArrayList<Customer>();

    customers.add(new Customer("1", "customer1", "some address 1", 20));
    customers.add(new Customer("2", "customer2", "some address 2", 45));
    customers.add(new Customer("3", "customer3", "some address 3", 36));

    return customers;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Customer 
{
    @XmlAttribute
    private String customerId;

    @XmlElement
    private String name;

    @XmlElement
    private String address;

    @XmlElement
    private int age;

    public Customer()
    {

    }

    public Customer(String customerId, String name, String address, int age)
    {
        this.customerId = customerId;
        this.name = name;
        this.address = address;
        this.age = age;
    }

    public String getCustomerId() 
    {
        return customerId;
    }

    public void setCustomerId(String customerId) 
    {
        this.customerId = customerId;
    }

    public String getName() 
    {
        return name;
    }

    public void setName(String name) 
    {
        this.name = name;
    }

    public String getAddress()
    {
        return address;
    }

    public void setAddress(String address)
    {
        this.address = address;
    }

    public int getAge() 
    {
        return age;
    }

    public void setAge(int age) 
    {
        this.age = age;
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<collection>
    <customer customerId="1">
        <name>customer1</name>
        <address>some address 1</address>
        <age>20</age>
    </customer>
    <customer customerId="2">
        <name>customer2</name>
        <address>some address 2</address>
        <age>45</age>
    </customer>
    <customer customerId="3">
        <name>customer3</name>
        <address>some address 3</address>
        <age>36</age>
    </customer>
</collection>
这将客户列表包装在
客户
元素中,而不是
集合

<?xml version="1.0" encoding="UTF-8"?>
<customers>
    <customer customerId="1">
        <name>customer1</name>
        <address>some address 1</address>
        <age>20</age>
    </customer>
    <customer customerId="2">
        <name>customer2</name>
        <address>some address 2</address>
        <age>45</age>
    </customer>
    <customer customerId="3">
        <name>customer3</name>
        <address>some address 3</address>
        <age>36</age>
    </customer>
</customers>

顾客1
一些地址1
20
顾客2
一些地址2
45
顾客3
一些地址3
36

与序列化的
客户
有什么关系?为什么要返回
客户列表
?这是什么?@Tichodroma请查看问题中的链接。我试图返回一个Customer对象列表:list,其结果应该是。。。。。。这张
客户名单是从哪里来的?你确定它不是空的吗?看,我已经更新了代码。CustomerList是CustomerService中的字段。服务是单身。同样,正如我在问题中提到的,列表不是空的,我已经检查过了。这也没有运气。还是空着。我从不同的方法返回内容类型为application/json的相同列表,并且可以在结果中看到完整的预期列表。我认为这意味着JAXB客户类可能有问题。您是否尝试过用@XmlElement而不是getter来注释属性?