我正在开发使用Jersey Rest for Services的项目。我有一个Project类,它有ArrayList of Activities。
package com.test.services.model;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Project {
public Project(){
ArrayList<Activity> activities = new ArrayList<Activity>();
Activity activity = new Activity();
activity.setId("act1");
activity.setName("Activity 1");
activities.add(activity);
activity = new Activity();
activity.setId("act2");
activity.setName("Activity 2");
activities.add(activity);
this.setActivities(activities);
}
private ArrayList<Activity> activities;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement(name="activities")
public ArrayList<Activity> getActivities() {
return activities;
}
public void setActivities(ArrayList<Activity> activities) {
this.activities = activities;
}
@XmlElement(name="Codes")
public ArrayList<String> getCodes() {
ArrayList<String> str = new ArrayList<String>();
str.add("Hello");
str.add("World");
return str;
}
private int id;
}
这就是我的XML的样子
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<projects>
<project>
<activities/> // Empty
<activities/> //Empty
<Codes>Hello</Codes>
<Codes>World</Codes>
<id>1</id>
<name>Project 1</name>
</project>
<project>
<activities/>
<activities/>
<Codes>Hello</Codes>
<Codes>World</Codes>
<id>2</id>
<name>Project 2</name>
</project>
<project>
<activities/>
<activities/>
<Codes>Hello</Codes>
<Codes>World</Codes>
<id>3</id>
<name>Project 3</name>
</project>
<project>
<activities/>
<activities/>
<Codes>Hello</Codes>
<Codes>World</Codes>
<id>4</id>
<name>Project 4</name>
</project>
</projects>
我添加了空的ArrayList代码;查看ArrayList是否有任何问题。我不确定为什么在我的XML中返回空活动。
任何帮助将不胜感激
谢谢, [R