【发布时间】: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 并删除 <com.eaio.uuid.UUID> 和相应的结束标签(只是标签)并保留内容,则 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