java - Xml反序列化不适用于Xstream

标签 java serialization xstream mime-message

我有一个正在序列化的以下课程


  org.springframework.integration.core.Message


进入文件如下:

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);
}


如果我运行UnMarshall.java,则会收到以下异常:




是否可以使用XStream序列化/反序列化Message对象?
如果没有,我还有什么其他选择?

线程“主”中的异常com.thoughtworks.xstream.converters.ConversionException:无法构造类java.lang.Class
    ----调试信息----
    消息:无法构造类java.lang.Class
    原因异常:java.lang.InstantiationException
    原因消息:javax.activation.ActivationDataFlavor
    类别:org.springframework.integration.message.GenericMessage
    required-type:javax.activation.ActivationDataFlavor
    路径:/org.springframework.integration.message.GenericMessage/payload/dh/dataContentHandler/dch/myDF
    行号:14
    -------------------------------
        在com.thoughtworks.xstream.converters.reflection.ExternalizableConverter.unmarshal(ExternalizableConverter.java:110)
        在com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:56)
        在com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:45)
        在com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:46)
        在com.thoughtworks.xstream.annotations.AnnotationReflectionConverter.unmarshallField(AnnotationReflectionConverter.java:66)
        在com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:188)
        在com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:125)
        在com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:56)
        在com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:45)
        在com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:46)
        在com.thoughtworks.xstream.annotations.AnnotationReflectionConverter.unmarshallField(AnnotationReflectionConverter.java:66)
        在com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:188)
        在com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:125)
        在com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:56)
        在com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:45)
        在com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:46)
        在com.thoughtworks.xstream.annotations.AnnotationReflectionConverter.unmarshallField(AnnotationReflectionConverter.java:66)
        在com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:188)
        在com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:125)
        在com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:56)
        在com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:45)
        在com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:46)
        在com.thoughtworks.xstream.annotations.AnnotationReflectionConverter.unmarshallField(AnnotationReflectionConverter.java:66)
        在com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:188)
        在com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:125)
        在com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:56)
        在com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:45)
        在com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:46)
        在com.thoughtworks.xstream.annotations.AnnotationReflectionConverter.unmarshallField(AnnotationReflectionConverter.java:66)
        在com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:188)
        在com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:125)
        在com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:56)
        在com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:45)
        在com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:46)
        在com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:117)
        在com.thoughtworks.xstream.core.ReferenceByXPathMarshallingStrategy.unmarshal(ReferenceByXPathMarshallingStrategy.java:29)
        在com.thoughtworks.xstream.XStream.unmarshal(XStream.java:846)
        在com.thoughtworks.xstream.XStream.unmarshal(XStream.java:833)
        在com.thoughtworks.xstream.XStream.fromXML(XStream.java:789)
        在org.springframework.integration.samples.mail.imapidle.DefaultEmailReceiverUtilService.main(DefaultEmailReceiverUtilService.java:103)
    引起原因:java.lang.InstantiationException:javax.activation.ActivationDataFlavor
        在java.lang.Class.newInstance0(Class.java:340)
        在java.lang.Class.newInstance(Class.java:308)
        在com.thoughtworks.xstream.converters.reflection.ExternalizableConverter.unmarshal(ExternalizableConverter.java:80)
        ...另外39个

最佳答案

我在搜索my XStream bug时发现了此问题。这里的问题是XStream依赖于ActivationDataFlavor声称是可外部化的事实。但要完全说明,它需要一个公共的无参数构造函数,而没有该构造函数,因此需要java.lang.InstantiationException。

好消息是XStream的作者解决了这个问题。因此,如果您使用XStream 1.5.0-SNAPSHOT,则不应再遇到此问题。

坏消息是,我还没有找到让Oracle注意到ActivationDataFlavorPrivateMLet都不完全尊重Externalizable接口的方法。没错,这个公共的无参数构造函数的要求非常隐蔽,但是正如您在StackTrace上面所看到的那样,它确实是必需的


  重建Externalizable对象时,将创建一个实例
  使用公共的无参数构造函数,然后使用readExternal方法
  叫。

关于java - Xml反序列化不适用于Xstream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14195397/

相关文章:

java - 将 SOAP 响应转换为 xstream 对象

java - 获取两个字符串列表中的公共(public)字符串对

java - XmlWebApplicationContext、AnnotationConfigWebApplicationContext 和 GenericWebApplicationContext 之间有什么区别

java - 反序列化一个包含一个元素的列表,直接作为元素本身,以防列表大小为 1(Json 格式)

无事件的 C# WPF 序列化 XAML

c# - Json.net 序列化/反序列化派生类型?

java - Xstream 在解码时不维护子元素的顺序

java - 使用java从服务器端的URL中提取参数

java - 在java中控制文本文件中的特定行

java - com.thoughtworks.xstream.converters.ConversionException