2013-08-22 104 views
1

只是嘗試了噴霧JSON,它似乎有問題找到我的JsonProtocols我已經設置。我有以下的依存關係:Marshaller沒有找到

"io.spray"    % "spray-servlet" % "1.2-M8", 
"io.spray"    % "spray-routing" % "1.2-M8", 
"io.spray"    % "spray-testkit" % "1.2-M8", 
"io.spray"    % "spray-json_2.10" % "1.2.5" 

以下代碼:

Content.scala

import spray.json.DefaultJsonProtocol 

case class Content(id:String, name: String, contentType: String, duration: Int) 

object MyJsonProtocol extends DefaultJsonProtocol { 
    implicit val contentFormat = jsonFormat4(Content) 
} 

我得到的地方,我在complete {}塊返回Content行了,錯誤的錯誤如下,代碼如下:

描述離子資源路徑位置類型 找不到類型spray.httpx.marshalling.Marshaller [內容] MyService.scala線32斯卡拉問題的證據參數

import akka.actor.Actor 
import spray.routing._ 
import spray.http._ 
import MediaTypes._ 
import spray.json.DefaultJsonProtocol 
import Content 
import MyJsonProtocol._ 

class MyServiceActor extends Actor with MyService{ 

    def actorRefFactory = context 

    def receive = runRoute(myRoute) 
} 

trait MyService extends HttpService { 
    val myRoute = 
    path("") { 
     get { 
     respondWithMediaType(`application/json`) { // XML is marshalled to `text/xml` by default, so we simply override here 
      complete { 
      new Content("1234", "Some Content", "YT", 60) 
      } 
     } 
     } 
    } 
} 

任何人都可以看到什麼問題隱含的價值?這是字面上的噴霧模板代碼在

灑噴霧JSON的東西

回答

7

的Json編組是SprayJsonSupport特質,所以只是將其導入範圍:

import spray.httpx.SprayJsonSupport._ 

而與此編組可以刪除respondWithMediaType(application/json)導致Json被封送到application/json媒體類型:

implicit def sprayJsonMarshaller[T](implicit writer: RootJsonWriter[T], printer: JsonPrinter = PrettyPrinter) = 
    Marshaller.delegate[T, String](ContentTypes.`application/json`) { value ⇒ 
    val json = writer.write(value) 
    printer(json) 
    } 
+0

非常好,謝謝Alex。我錯過了文檔中的關鍵點,是否在spray.io上? – ThaDon