I am trying out RESTEasy webservices. I have written simple service to return List of JAXB Customer objects and expecting the returned xml to be collection of Customer tags under Collection tag. But what I am getting is <Collection/>
, means an empty collection.
我正在尝试RESTEasy webservices。我编写了简单的服务来返回JAXB Customer对象的List,并期望返回的xml是Collection标签下的Customer标签的集合。但我得到的是
My code is :
我的代码是:
Customer Service
@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
}
}
Customer (JAXB Object)
客户(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;
}
}
I am able to get single Customer with this Service, which works just fine :
我可以通过此服务获得单个客户,这可以正常工作:
@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;
}
I have made the service singleton so the list is not empty, that for sure. Even I have debugged the code to confirm that the list is not empty. I tried with the array also and same is happening.
我已经使服务单例,所以列表不是空的,这是肯定的。即使我已调试代码以确认列表不为空。我也尝试了阵列,同样的事情发生了。
Here is the thing, I am reading from: http://docs.jboss.org/resteasy/docs/2.3.0.GA/userguide/html/Built_in_JAXB_providers.html#JAXB_Collections
以下是我正在阅读的内容:http://docs.jboss.org/resteasy/docs/2.3.0.GA/userguide/html/Built_in_JAXB_providers.html#JAXB_Collections
I am using
我在用
- WebSphere Application Server 8.0
- RESTEasy 2.2.3GA
WebSphere Application Server 8.0
1 个解决方案
#1
4
I was able to make the serialization work with your Customer POJO by making the following changes:
通过进行以下更改,我能够使您的客户POJO进行序列化:
- Moved the
@XmlElement
annotations to the fields - Added
@XmlAccessorType(XmlAccessType.FIELD)
annotation to tell JAXB to bind by fields and ignore getters/setters.
将@XmlElement注释移动到字段
添加了@XmlAccessorType(XmlAccessType.FIELD)注释,告诉JAXB按字段绑定并忽略getter / setter。
Method:
@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;
}
Customer 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;
}
}
Output 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>
What if you want to change the name of the wrapping element?
If you don't like the xml list being returned wrapped in a collection
element and want to change the name you can add the @Wrapped
annotation to your resource method like so:
如果您不喜欢返回包含在集合元素中的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;
}
This wraps the list of customers in a customers
element instead of collection
.
这将客户列表包含在customers元素中而不是集合中。
<?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
4
I was able to make the serialization work with your Customer POJO by making the following changes:
通过进行以下更改,我能够使您的客户POJO进行序列化:
- Moved the
@XmlElement
annotations to the fields - Added
@XmlAccessorType(XmlAccessType.FIELD)
annotation to tell JAXB to bind by fields and ignore getters/setters.
将@XmlElement注释移动到字段
添加了@XmlAccessorType(XmlAccessType.FIELD)注释,告诉JAXB按字段绑定并忽略getter / setter。
Method:
@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;
}
Customer 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;
}
}
Output 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>
What if you want to change the name of the wrapping element?
If you don't like the xml list being returned wrapped in a collection
element and want to change the name you can add the @Wrapped
annotation to your resource method like so:
如果您不喜欢返回包含在集合元素中的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;
}
This wraps the list of customers in a customers
element instead of collection
.
这将客户列表包含在customers元素中而不是集合中。
<?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>