尝试访问Scala中的bean属性时,snamkeyaml中的ClassCastException

时间:2018-07-08 22:17:18

标签: scala snakeyaml

这是我要在下面解析的Yaml:

默认值:&defaults   OCR:    包括:true

ssps:   “ APP”:     injectpixela:      包括:真实      不包括:[UIBK,UILR]     impressiontrackurls:       包括:真实       不包括:[UIBK,UILR]     <<:*默认值

下面是我要解析的类:

import scala.beans.{BeanInfo, BeanProperty}

@BeanInfo
class SSPSyncUrl {
  @BeanProperty var ssps: java.util.HashMap[String, java.util.HashMap[String, SyncPixel]] = new java.util.HashMap[String, java.util.HashMap[String, SyncPixel]]
}

@BeanInfo
class SyncPixel {
  @BeanProperty var included : Boolean = false
  @BeanProperty var excludes = new java.util.ArrayList[String]
}

This is the way I'm trying to load it:

val trackingPixelFileName1 = s"/config/ssp.yml"
val pixelStream1: InputStream = getClass.getResourceAsStream(trackingPixelFileName1)
pixelContent = scala.io.Source.fromInputStream(pixelStream1).mkString
try {
  val resp = new Representer
  resp.getPropertyUtils.setSkipMissingProperties(true)
  val yaml1 = new Yaml(new Constructor(classOf[SSPSyncUrl]), resp)
  val data: SSPSyncUrl = yaml1.load(pixelContent).asInstanceOf[SSPSyncUrl]

  print("excludes value " + data.ssps.get("APP").get("ocr").getIncluded)

在上面的行中抛出classcastexception。 “ LinkedHashMap无法转换为SyncPixel”

为什么我不能访问bean属性?您建议进行此项更改有哪些变化? 有任何想法吗?

谢谢

0 个答案:

没有答案