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

时间:2013-07-12 12:20:24

标签: xml jaxb jax-rs resteasy

我正在尝试RESTEasy webservices。我编写了简单的服务来返回JAXB Customer对象的List,并期望返回的xml是Collection标签下的Customer标签的集合。但我得到的是<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 Application Server 8.0
  • RESTEasy 2.2.3GA

1 个答案:

答案 0 :(得分:4)

通过进行以下更改,我能够使您的客户POJO进行序列化:

  • @XmlElement注释移至字段
  • 添加@XmlAccessorType(XmlAccessType.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;
}

客户POJO:

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

<?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>

如果要更改包装元素的名称,该怎么办?

如果您不喜欢包含在collection元素中的xml列表并希望更改名称,则可以将@Wrapped注释添加到资源方法中,如下所示:

@GET
@Path("/customers")
@Produces(MediaType.APPLICATION_XML)
@Wrapped(element = "customers")
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;
}

这会将客户列表包含在customers元素中,而不是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>