为什么在使用JavaUUID的情况下完成请求会导致编译错误?

时间:2014-04-01 17:46:56

标签: scala akka spray

我正在与Akka和Spray合作开发一个项目。除了使用以下路由处理的GET之外,它工作正常:

class UserRoot extends Directives with DefaultJsonFormats with PerRequestCreator {
  val route =
    path("users" / JavaUUID / "activities") { userId =>
      get {
        complete {
          createActorPerRequest(new StringUUID(userId), Props[LogicGetActivitiesFromUser])
        }
      }
    }

  def createActorPerRequest(entity: Entity, target: Props): Route =
     context => perRequest(context, target, Work(entity))
}

StringUUID类:

case class StringUUID(id: String) extends Entity // I also tried UUID instead of String

以上代码错误如下:

error: type mismatch;
 found   : java.util.UUID
 required: String
       createActorPerRequest(new StringUUID(userId), Props[LogicGetActivitiesFromUser])

但如果我添加import reflect.ClassTag(我在互联网上发现了这个),它会给我另一个错误:

error: could not find implicit value for evidence parameter of type spray.httpx.marshalling.Marshaller[spray.routing.RequestContext => Unit]
createActorPerRequest(new StringUUID(userId.toString), Props[LogicGetActivitiesFromUser])

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

看看这个Example。它没有complete

以下route应修正错误:

val route = 
  path("pets") {
    get {
      petsWithOwner {
        GetPetsWithOwners(List("Lassie"))
      }
    }
  }

答案 1 :(得分:1)

问题是类型不匹配,因此Scala编译器(不是Spray或Akka或其他任何东西)将其报告为编译错误。

route代码的第一个版本中,请注意userId的类型 - 它是java.util.UUID,正如Scala编译器所说的那样。在java.util.UUID输入参数的String中,您不能使用StringUUID类型的val id

在您使用userId.toString的代码的第二个版本中,类型Stringid输入参数的预期类型匹配。这导致了另一个问题,即createActorPerRequest请求complete无法隐瞒complete的回复。

您应该让产生的演员到ask响应或等待演员的响应(使用complete模式)并将其作为来自complete的响应传递。这也需要一个marshaller(就像演员receive中{{1}}内的代码一样。)