4

我正在尝试 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
4

1 回答 1

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>

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

如果您不喜欢将返回的 xml 列表包装在collection元素中并希望更改名称,则可以将@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>
于 2013-07-12T16:29:06.183 回答