当需要@XmlJavaTypeAdapter时,如何(取消)使用JAXB / SCALA序列化对象?

时间:2011-08-29 10:39:44

标签: scala annotations jaxb

我需要帮助使用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中序列化复杂对象层次结构?

感谢。

1 个答案:

答案 0 :(得分:0)

我不确定如何解决你的第一个问题。注释案例类可能会有问题吗?可以尝试使用非案例类。但同样非常不确定。可能与这个问题有关吗?

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/ ...