我正在使用Spray构建REST API.我的一个JSON数据类型是递归的:
case class Container(id: String,
name: String,
read_only: Boolean,
containers: List[Container],
content: List[Content])
object PeriusJsonProtocol extends DefaultJsonProtocol {
implicit val contentFormat = jsonFormat8(Content)
implicit val containerFormat: JsonFormat[Container] = lazyFormat(jsonFormat5(Container))
}
Run Code Online (Sandbox Code Playgroud)
在HttpService中完成路由时,我收到以下错误:
Error:(49, 18) could not find implicit value for parameter um: spray.httpx.unmarshalling.FromRequestUnmarshaller[Container]
entity(as[Container]) { container =>
^
Error:(49, 18) not enough arguments for method as: (implicit um: spray.httpx.unmarshalling.FromRequestUnmarshaller[Container])spray.httpx.unmarshalling.FromRequestUnmarshaller[Container].
Unspecified value parameter um.
entity(as[Container]) { container =>
^
Run Code Online (Sandbox Code Playgroud)
HttpService看起来像这样:
import akka.actor.Actor
import spray.routing._
import spray.httpx.SprayJsonSupport._
import PeriusJsonProtocol._
class RestServiceActor extends …Run Code Online (Sandbox Code Playgroud) 我有一个关于通过使用Spray-Akka将对象解组到Json的问题。
当我想使用返回Future [List [Person]]的actor时,它将无法正常工作。
如果我直接使用dao对象,则可以使用。
这是我的代码:
人道斯卡拉
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
case class Person(id: Int, name: String, surname: String)
object PersonDao {
def getAll: Future[List[Person]] = Future {
List[Person](Person(1, "Bilal", "Alp"), Person(2, "Ahmet", "Alp"))
}
}
Run Code Online (Sandbox Code Playgroud)
EntityServiceActor.scala
import akka.actor.Actor
import com.bilalalp.akkakafka.model.PersonDao
import com.bilalalp.akkakafka.service.ServiceOperation.FIND_ALL
object ServiceOperation {
case object FIND_ALL
}
class EntityServiceActor extends Actor {
override def receive: Receive = {
case FIND_ALL => PersonDao.getAll
}
}
Run Code Online (Sandbox Code Playgroud)
ServerSupervisor.scala
import akka.actor.{Actor, ActorRefFactory}
import com.bilalalp.akkakafka.webservice.TaskWebService
import spray.routing.RejectionHandler.Default
class ServerSupervisor extends Actor with …Run Code Online (Sandbox Code Playgroud) 我试图在运行时使用特征聚合路由,到目前为止我有
object SMController {
def aggregateRoutes(actorSystem: ActorSystem): List[Route] = {
val runtimeMirror = universe.runtimeMirror(getClass.getClassLoader)
val reflections = new Reflections("com.example.api")
val subclasses = reflections.getSubTypesOf(classOf[Routable])
val routesList = new ListBuffer[Route]()
for (i <- subclasses) {
val module = runtimeMirror.staticModule(i.getName)
val obj = runtimeMirror.reflectModule(module)
val someTrait: Routable = obj.instance.asInstanceOf[Routable]
routesList += someTrait.getAllRoutes(actorSystem)
}
routesList.toList
}
}
Run Code Online (Sandbox Code Playgroud)
显然上面的代码不起作用,因为无法将项目列表传递给Http().bindAndHandle.
所以我的问题是,我怎么能解析List[Routes]到一个Http().bindAndHandle接受或者我怎么能动态地从子类加载路线Routable?
从spray.io文档页面:
color提取参数“ color”的值作为字符串
color.?提取参数“ color”的可选值作为Option [String]
amount.as[Int]将参数“ amount”的值提取为Int,就需要一个匹配的反序列化器才能使其正常工作(另请参阅解组)
那么如何解析可选Int值?喜欢的东西amount.?.as[Int]似乎并没有工作时。