【问题标题】:How to (de-)serialize an object with JAXB/SCALA when an @XmlJavaTypeAdapter is needed?当需要 @XmlJavaTypeAdapter 时,如何使用 JAXB/SCALA (反)序列化对象?
【发布时间】:2011-08-29 10:39:44
【问题描述】:

我需要帮助使用 JAXB 来(反)序列化 JTS Library 的多边形:

实施时

class PolygonAdapter extends XmlAdapter[String,Polygon] {...}

并注释多边形字段,如:

@XmlRootElement(name = "Room")
case class Room(@XmlAttribute name: String, description: String,
                @XmlJavaTypeAdapter(classOf[PolygonAdapter]) polygon: Polygon) {...}

我收到标记为classOf[PolygonAdapter]) 的部分报告以下错误:

Multiple markers at this line
- type mismatch; found : java.lang.Class[room.PolygonAdapter](classOf[room.PolygonAdapter])     required: java.lang.Class[_ <: 
 javax.xml.bind.annotation.adapters.XmlAdapter]
- annotation argument needs to be a constant; found: classOf[PolygonAdapter]
- annotation argument needs to be a constant; found: classOf{<null>}[PolygonAdapter]{<null>}
- type mismatch; found : java.lang.Class[room.PolygonAdapter](classOf[room.PolygonAdapter])     required: java.lang.Class[_ <: 
 javax.xml.bind.annotation.adapters.XmlAdapter]
- annotation argument needs to be a constant; found: classOf{<null>}[PolygonAdapter]{<null>}

我的问题:

  • 我该如何解决这个问题?
  • 有没有比使用 JAXB 更好的方法来(反)序列化 scala 中的复杂对象层次结构?

谢谢。

【问题讨论】:

    标签: scala annotations jaxb


    【解决方案1】:

    我不确定如何解决您的第一个问题。注释案例类可能有问题吗?可以尝试使用非案例类。但再次对此非常不确定。可能与这个 SO 问题有关?

    scala: annotating accessor methods

    但是,您看过 Scala 的内置 XML 功能吗?

    http://www.ibm.com/developerworks/library/x-scalaxml/

    http://www.scala-lang.org/node/131

    http://www.codecommit.com/blog/scala/working-with-scalas-xml-support

    编辑:如果您想避免使用 Scala 的内置功能,可能值得一看:http://scalaxb.org/...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-14
      • 1970-01-01
      • 2017-08-02
      • 1970-01-01
      • 2012-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多