I have a problem with java generics and xstream when deserializing a Xml.
This is the main DTO
@XStreamAlias("OBJECTX")
public class ObjectX<T> {
@XStreamAlias("DATA")
private T t;
... getter and setter ...
}
This is the Data object inside the main DTO
@XStreamAlias("DATA")
public class FolderXml {
@XStreamAlias("DIGITION_NAME")
private String digitionName;
@XStreamAlias("FOLDER_ID")
private int folderId;
...getters and setters...
}
This is the Xml example to deserialize
<?xml version="1.0" encoding="UTF-8"?>
<OBJECTX>
<DATA>
<DIGITION_NAME>TVC_ACT2</DIGITION_NAME>
<FOLDER_ID>1234</FOLDER_ID>
</DATA>
</OBJECTX>
This is the actual code
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);
The exception is
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
-------------------------------
Modify your code to add default implementation for Generics.
StaxDriver staxDriver = new StaxDriver(new NoNameCoder());
XStream xstream = new XStream(staxDriver);
xstream.processAnnotations(ObjectX.class);
xstream.processAnnotations(FolderXml.class);
xstream.alias("DATA", FolderXml.class);
xstream.addDefaultImplementation(FolderXml.class,ObjectX.class.getDeclaredField("t").getType());
ObjectX<FolderXml> obj= (ObjectX<FolderXml>) xstream.fromXML(xml);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With