简体   繁体   中英

Xml Deserilization is not working using Xstream

I have a following class which is serializing

org.springframework.integration.core.Message

into file as follows:

Sample.java

public static void receive(Message<?> message) throws IOException, MessagingException
{
    XStream xStream = new XStream();
    File file = new File("c:/temp/temp.xml");
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    xStream.toXML(message,fileOutputStream);
}

UnMarshall.java

public static void main(String[] args) throws IOException, MessagingException, ClassNotFoundException
{
    XStream xStream = new XStream();
    File file = new File("c:/temp/temp.xml");
    FileInputStream fileOutputStream = new FileInputStream(file);
    Message message = (Message) xStream.fromXML(fileOutputStream);
}

If I run UnMarshall.java then I am getting the following exception:

Question

  • Is it possible to do serialize/deserialize Message object with XStream?
  • If not what other options I have?

    Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: Cannot construct class java.lang.Class ---- Debugging information ---- message : Cannot construct class java.lang.Class cause-exception : java.lang.InstantiationException cause-message : javax.activation.ActivationDataFlavor class : org.springframework.integration.message.GenericMessage required-type : javax.activation.ActivationDataFlavor path : /org.springframework.integration.message.GenericMessage/payload/dh/dataContentHandler/dch/myDF line number : 14 ------------------------------- at com.thoughtworks.xstream.converters.reflection.ExternalizableConverter.unmarshal(ExternalizableConverter.java:110) at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:56) at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:45) at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:46) at com.thoughtworks. xstream.annotations.AnnotationReflectionConverter.unmarshallField(AnnotationReflectionConverter.java:66) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:188) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:125) at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:56) at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:45) at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:46) at com.thoughtworks.xstream.annotations.AnnotationReflectionConverter.unmarshallField(AnnotationReflectionConverter.java:66) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:188) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflect ionConverter.java:125) at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:56) at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:45) at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:46) at com.thoughtworks.xstream.annotations.AnnotationReflectionConverter.unmarshallField(AnnotationReflectionConverter.java:66) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:188) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:125) at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:56) at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:45) at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:46) at com.thoughtworks.xstream. annotations.AnnotationReflectionConverter.unmarshallField(AnnotationReflectionConverter.java:66) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:188) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:125) at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:56) at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:45) at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:46) at com.thoughtworks.xstream.annotations.AnnotationReflectionConverter.unmarshallField(AnnotationReflectionConverter.java:66) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:188) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConve rter.java:125) at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:56) at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:45) at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:46) at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:117) at com.thoughtworks.xstream.core.ReferenceByXPathMarshallingStrategy.unmarshal(ReferenceByXPathMarshallingStrategy.java:29) at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:846) at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:833) at com.thoughtworks.xstream.XStream.fromXML(XStream.java:789) at org.springframework.integration.samples.mail.imapidle.DefaultEmailReceiverUtilService.main(DefaultEmailReceiverUtilService.java:103) Caused by: java.lang.InstantiationException: javax.activation.ActivationDataFlavor at java.lang.Class.newInstance0(Class.java:340) at java.lang.Class.newInstance(Class .java:308) at com.thoughtworks.xstream.converters.reflection.ExternalizableConverter.unmarshal(ExternalizableConverter.java:80) ... 39 more

I found this issue when I was googling for my XStream bug . The problem here is that XStream is relying on the fact that ActivationDataFlavor claims to be Externalizable. But to fully be that it needs a public no-arg constructor, which is does not have, hence the java.lang.InstantiationException.

The good news is that the author of XStream worked around this. So if you use XStream 1.5.0-SNAPSHOT you should no longer run into this issue.

The bad news is that I've not yet found a way to get Oracle to notice that both ActivationDataFlavor and PrivateMLet do not fully respect the Externalizable interface. True, this public no-arg constructor requirement is quite hidden, but as you can see in above StackTrace, it's really needed

When an Externalizable object is reconstructed, an instance is created using the public no-arg constructor, then the readExternal method called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM