如何使用Spray将Scala数组序列化为HTTP响应中的JSON数组?

时间:2015-12-23 19:46:28

标签: scala spray spray-json

我是一个Spray newby,有点精通Scala,虽然Spray文档总体上非常好,但我找不到任何相关的例子来解决我的问题。我正在尝试做一些非常简单的事情:使用json4s或spray-json将我的类型的Scala数组序列化(或Marshall)到json数组。在这个例子中,我使用spray-json,以及build.sbt中的以下条目:

libraryDependencies ++= Seq(
  "io.spray" %%  "spray-json" % "1.3.2"
)

以下代码会产生以下编译器错误:

[error] /Users/scott/integration/src/main/scala/com/anomaly/rest/AnomalyService.scala:101: type mismatch;
[error]  found   : Array[com.anomaly.model.KDEOutlier]
[error]  required: spray.httpx.marshalling.ToResponseMarshallable
[error]             complete(outlierService.getOutliersOverTime(startTime, endTime))

/* request handling actor and service */

package com.anomaly.rest

import akka.actor.Actor
import akka.event.slf4j.SLF4JLogging
import spray.routing._
import spray.http._
import spray.json._
import DefaultJsonProtocol._
import MediaTypes._

import anomaly.model.KDEOutlier
import anomaly.dal.KDEOutliersDAO

class AnomalyServiceActor extends Actor with AnomalyService {
  def actorRefFactory = context
  def receive = runRoute(anomaliesRoute)
}

trait AnomalyService extends HttpService with SLF4JLogging {
  val outlierService = KDEOutliersDAO
  val anomaliesRoute = respondWithMediaType(MediaTypes.`application/json`) {
    path("anomalies/overtime ") {
      get {
        parameters('startTime.as[Long], 'endTime.as[Long]) { (startTime, endTime) =>
            complete(outlierService.getOutliersOverTime(startTime, endTime))
        }
      }
    }
  }
}


/* model */

package com.anomaly.model
import java.util.Date

case class KDEOutlier(date: Long, score: Double)


/* dao service called by request handler service */

package com.anomaly.dal

import java.util.Date
import com.anomaly.model.KDEOutlier
import com.anomaly.model.KDERawOutlier

object KDEOutliersDAO {  
    def getOutliersOverTime(startTime: Long, endTime: Long): Array[KDEOutlier] = {
        Array(KDEOutlier(Date.UTC(2013,5,2,0,0,0),0.7695), KDEOutlier(Date.UTC(2013,5,3,0,0,0),0.7648),
                KDEOutlier(Date.UTC(3,5,4,0,0,0),0.7645), KDEOutlier(Date.UTC(2013,5,5,0,0,0),0.7638),
                KDEOutlier(Date.UTC(2013,5,6,0,0,0),0.8549), KDEOutlier(Date.UTC(2013,5,7,0,0,0),0.9562),
                KDEOutlier(Date.UTC(2013,5,9,0,0,0),0.7574), KDEOutlier(Date.UTC(2013,5,10,0,0,0),0.7543))
    }
}

1 个答案:

答案 0 :(得分:0)

您还需要定义隐式RootJsonFormat(例如jsonFormat2(KDEOutlier))和import spray.httpx.SprayJsonSupport._

package com.anomaly.rest

import com.scalakata._

import akka.actor.Actor
import akka.event.slf4j.SLF4JLogging
import spray.routing._
import spray.http._
import spray.json._
import spray.httpx.SprayJsonSupport._
import DefaultJsonProtocol._
import MediaTypes._
import java.util.Date

case class KDEOutlier(date: Long, score: Double)

class AnomalyServiceActor extends Actor with AnomalyService {
  def actorRefFactory = context
  def receive = runRoute(anomaliesRoute)
}

trait AnomalyService extends HttpService with SLF4JLogging {

  implicit val outlierFormat = jsonFormat2(KDEOutlier)

  val anomaliesRoute = respondWithMediaType(MediaTypes.`application/json`) {
    path("anomalies/overtime ") {
      get {
        parameters('startTime.as[Long], 'endTime.as[Long]) { (startTime, endTime) => {
          val result = Array(KDEOutlier(Date.UTC(2013,5,2,0,0,0),0.7695), KDEOutlier(Date.UTC(2013,5,3,0,0,0),0.7648),
                KDEOutlier(Date.UTC(3,5,4,0,0,0),0.7645), KDEOutlier(Date.UTC(2013,5,5,0,0,0),0.7638),
                KDEOutlier(Date.UTC(2013,5,6,0,0,0),0.8549), KDEOutlier(Date.UTC(2013,5,7,0,0,0),0.9562),
                KDEOutlier(Date.UTC(2013,5,9,0,0,0),0.7574), KDEOutlier(Date.UTC(2013,5,10,0,0,0),0.7543))
          complete(result)
        }                                                     }
      }
    }
  }
}

您可以在http://scalakata.com

上试用此代码段