【问题标题】:Explain mysterious class name in XStream XML stream解释 XStream XML 流中的神秘类名
【发布时间】:2015-06-04 02:09:59
【问题描述】:

当通过 RabbitMQ 发送数据时,我正在使用 XStream 1.4.8 与 XML 进行序列化。在某些情况下,类名会添加到生成的 XML 中,这会破坏反序列化。对我来说最难解释的问题是,在生产中,某个XML值不会产生异常,而在开发中,我会得到一个异常。

严重序列化的 XML:

<company.events.twitter.InteractionReceivedFromTwitter4>
    <header>
        <id serialization="custom">
            <com.eaio.uuid.UUID>
                <long>4426172843343876581</long>
                <long>-7002731889965487449</long>
            </com.eaio.uuid.UUID>
        </id>
        <date>1433342326156</date>
        <data class="scala.collection.immutable.Map$EmptyMap$"/>
    </header>
    <!-- more data fields -->
</company.events.twitter.InteractionReceivedFromTwitter4>

此 XML 无法序列化,但 是从 XStream 1.4.8 生成的。如果我编辑 XML 并删除 &lt;com.eaio.uuid.UUID&gt; 和相应的结束标签(只是标签)并保留内容,则 XML 将正确反序列化。

我想了解添加这种额外符号的方式和原因。我用来序列化和反序列化的代码:

class XStreamSerializer extends Serializer {
    private val xstream = new XStream

    def serialize(x: AnyRef) = xstream.toXML(x).getBytes

    def deserialize(data: Array[Byte], contentType: String) = {
        if (contentType != serializationContentType)
            throw new IllegalArgumentException("Can only decode " + serializationContentType + ", received " + contentType)

        val res = xstream.fromXML(new ByteArrayInputStream(data))

        Option(res)
    }

    def serializationContentType = "application/xml;charset=utf-8"
}

case class MessageHeader( id: UUID,  date: Long,  data: Map[String, String])
case class InteractionReceivedFromTwitter4(header: MessageHeader,
                                           correlationId: UUID,
                                           interaction: InteractionMessage,
                                           persona: InteractionPersona)

在开发过程中,我从未见过额外的类提及。我可以通过删除字符串“”和关闭标签来“解决”这个问题,但我想了解。

我实际上并不关心在线交换的数据格式。类名是否存在对我来说并不重要。

【问题讨论】:

    标签: xstream


    【解决方案1】:

    我设法找到了问题所在。我在反序列化端使用 Scala 2.9.0.1,在序列化端使用 Scala 2.11.4。 Scala 2.11 生成的 Java 对象在结构上与 Scala 2.9.0.1 Java 对象不同,这应该可以解释差异。

    我还发现了我使用的 UUID 库的一个差异,反序列化方面为 3.4,序列化方面为 3.2。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多