只是尝试了喷雾json,它似乎有问题找到我的JsonProtocols我已经设置.我有以下依赖项:
"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"
Run Code Online (Sandbox Code Playgroud)
以下代码:
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)
}
Run Code Online (Sandbox Code Playgroud)
我Content
在complete {}
块中返回的行上出现错误,错误如下,代码在它下面:
说明资源路径位置类型找不到类型为spray.httpx.marshalling.Marshaller [Content] MyService.scala line 32 Scala问题的证据参数的隐式值
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)
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
谁能看到有什么不对劲?这就是喷涂模板代码,喷涂了喷涂json的东西
Json marshaller在SprayJsonSupport特性中,所以只需将其导入范围:
import spray.httpx.SprayJsonSupport._
Run Code Online (Sandbox Code Playgroud)
使用这个marshaller你可以删除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)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3416 次 |
最近记录: |