1

我有一个正在序列化的类

org.springframework.integration.core.Message

进入文件如下:

示例.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 序列化/反序列化消息对象?
  • 如果不是,我还有什么其他选择?

    线程“主”com.thoughtworks.xstream.converters.ConversionException 中的异常:无法构造类 java.lang.Class ---- 调试信息 ---- 消息:无法构造类 java.lang.Class 原因-异常:java。 lang.InstantiationException 原因消息:javax.activation.ActivationDataFlavor 类:org.springframework.integration.message.GenericMessage 所需类型:javax.activation.ActivationDataFlavor 路径:/org.springframework.integration.message.GenericMessage/payload/dh/dataContentHandler /dch/myDF 行号:14 ------------------------------- com.thoughtworks.xstream.converters.reflection。 com.thoughtworks.xstream.core 上的 ExternalizableConverter.unmarshal(ExternalizableConverter.java:110)。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) 在 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.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:188) 在 com.thoughtworks.xstream.java:66) 在 com.thoughtworks.xstream.annotations.AnnotationReflectionConverter.unmarshallField(AnnotationReflectionConverter.java:66) 在 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.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) at org.springframework.integration.samples.mail.imapidle.DefaultEmailReceiverUtilService.main(DefaultEmailReceiverUtilService.java:103) 原因:java.lang.InstantiationException:java.lang 中的 javax.activation.ActivationDataFlavor。 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更多的newInstance(Class.java:308) at com.thoughtworks.xstream.converters.reflection.ExternalizableConverter.unmarshal(ExternalizableConverter.java:80) ... 39 更多newInstance(Class.java:308) at com.thoughtworks.xstream.converters.reflection.ExternalizableConverter.unmarshal(ExternalizableConverter.java:80) ... 39 更多

4

1 回答 1

0

当我在谷歌上搜索我的 XStream 错误时,我发现了这个问题。这里的问题是 XStream 依赖于 ActivationDataFlavor 声称是 Externalizable 的事实。但要完全做到这一点,它需要一个公共的无参数构造函数,它没有,因此 java.lang.InstantiationException。

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

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

重构 Externalizable 对象时,会使用公共无参数构造函数创建实例,然后调用 readExternal 方法。

于 2016-01-26T14:20:02.580 回答