我试图序列化一些实体,似乎XStream根本不支持继承。 XStream版本:1.4.5
我的测试代码:
public class SOverflow {
public static void main(String[] args) {
XStream xStream = new XStream(new StaxDriver());
xStream.processAnnotations(Animal.class);
xStream.processAnnotations(Dog.class);
String s = xStream.toXML(new Dog());
System.out.println(s);
}
}
@XStreamAlias("animal")
class Animal {
@XStreamAlias("weight")
private String weight = "10";
public Animal() {
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
}
@XStreamAlias("dog")
class Dog extends Animal {
@XStreamAlias("name")
private String name = "Yo-yo";
public Dog() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
结果我得到了:
<?xml version="1.0" ?><dog><name>Yo-yo</name></dog>
没有amotations的默认行为是相同的。
可能错过了某些内容,或者根本不支持?