如何在同一元素上使用多个@XStreamAlias注释。当我这样做时:
@XStreamAlias("alias1")
@XStreamAlias("alias2")
class ABC{
//
}
我收到编译错误。有没有办法实现这个目标?
答案 0 :(得分:-1)
以编程方式为例:
import java.io.InputStream;
import java.util.List;
import org.junit.Test;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import lombok.Data;
public class MultiAlias {
@Test
public void smokeTest() {
InputStream file = MultiAlias.class.getResourceAsStream("MultiAliasTest.xml");
XStream xStream = new XStream();
xStream.ignoreUnknownElements();
xStream.processAnnotations(Parent.class);
xStream.alias("child", Child.class);
xStream.alias("kind", Child.class);
Parent parent = (Parent) xStream.fromXML(file);
System.out.println(parent);
}
@XStreamAlias("parent")
@Data
public class Parent {
@XStreamAsAttribute
private String name;
private List<Child> children;
}
@Data
public class Child {
private String name;
}
}
XML:
<?xml version="1.0" encoding="UTF-8"?>
<parent name="Adam">
<age>33</age>
<children>
<child>
<name>Iva</name>
</child>
<kind>
<name>Peter</name>
</kind>
</children>
</parent>
输出:
MultiAlias.Parent(name=Adam, children=[MultiAlias.Child(name=Iva), MultiAlias.Child(name=Peter)])
答案 1 :(得分:-1)
我认为,正如马克所说,注释是不可能的(在网络上找不到任何东西)。 在我的特殊情况下,作为一个walk arround,我所做的是从基类扩展(Vivek示例中的ABC)并将它们自己的注释放在子类中并使用这些类。芽解决方案取决于您的代码实现。 此致.-
ABC班
@XStreamAlias( “alias1”) DFG延伸ABC
@XStreamAlias( “别名2”) XYZ扩展了ABC