2015-01-05 52 views
3

Registration.scala如何使用噴霧JSON庫的toJSON申請的情況下,類的實例

package model 

import akka.actor.Actor 
import spray.json._ 
import DefaultJsonProtocol._ 

case class Registration(
    system: String, 
    identity: String) 

object RegistrationProtocol extends DefaultJsonProtocol { 
    implicit val adsRegistrationFormat = jsonFormat2(Registration) 
} 

RegistrationService.scala

import akka.actor.{Props, ActorLogging, Actor} 
import model.Registration 

object RegistrationsService { 
    case class PostRegistrationMessage(registration: Registration) 

    def props(property: String) = Props(classOf[RegistrationsService], property) 
} 

class RegistrationsService(property: String) extends Actor with ActorLogging { 
    import RegistrationsService._ 

    def receive = { 
    case PostRegistrationMessage(registration) => { 
     import model.RegistrationProtocol._ 

     val json = registration.toJson 

    } 
    } 
} 

誰能幫我明白這是爲什麼有編譯錯誤而失敗「value toJson不是model.Registration的成員」以及如何解決它。它如果在上面的代碼的最後一行,這是失敗的「VAL JSON = registration.toJson

回答

4

您需要導入噴霧在RegistrationService.scala提供過

import model.RegistrationProtocol._ 
    import spray.json._ 
+0

呀隱式操作,那工作。謝謝。我已經導入了RegistrationProtocol._,缺少的是'import spray.json._' –

相關問題