在反序列化Xml时,我遇到了java泛型和xstream的问题。
这是主要的DTO
@XStreamAlias("OBJECTX")
public class ObjectX<T> {
@XStreamAlias("DATA")
private T t;
... getter and setter ...
}
这是主DTO中的Data对象
@XStreamAlias("DATA")
public class FolderXml {
@XStreamAlias("DIGITION_NAME")
private String digitionName;
@XStreamAlias("FOLDER_ID")
private int folderId;
...getters and setters...
}
这是反序列化的Xml示例
<?xml version="1.0" encoding="UTF-8"?>
<OBJECTX>
<DATA>
<DIGITION_NAME>TVC_ACT2</DIGITION_NAME>
<FOLDER_ID>1234</FOLDER_ID>
</DATA>
</OBJECTX>
这是实际代码
StaxDriver staxDriver = new StaxDriver(new NoNameCoder());
XStream xstream = new XStream(staxDriver);
xstream.processAnnotations(ObjectX.class);
xstream.alias("DATA", FolderXml.class);
ObjectX<FolderXml> obj= (ObjectX<FolderXml>) xstream.fromXML(xml);
例外是
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field java.lang.Object.DIGITION_NAME
---- Debugging information ----
field : DIGITION_NAME
class : java.lang.Object
required-type : java.lang.Object
converter-type : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path : /OBJECTX/DATA/DIGITION_NAME
line number : 4
class[1] : cat.ccma.digition.digitionservicecontroller.objects.base.ObjectX
version : null
-------------------------------
答案 0 :(得分:1)
修改代码以添加Generics的默认实现。
Bar