Scala 为什么我的路线不识别我的协议

Scala 为什么我的路线不识别我的协议,scala,akka-http,spray-json,Scala,Akka Http,Spray Json,我注意到,在尝试为我的案例类编写jsonProtocol时,嵌套的案例类出现了错误。然而,如果我将case类解耦,只创建一个包含所有字段的大型case类,它就会工作得很好 case class Invited(invited:Array[Int]) case class Event(eventName:String,eventID:Int,invited: Invited) object jsonProtocol extends DefaultJsonProtocol { implicit

我注意到,在尝试为我的案例类编写jsonProtocol时,嵌套的案例类出现了错误。然而,如果我将case类解耦,只创建一个包含所有字段的大型case类,它就会工作得很好

case class Invited(invited:Array[Int])
case class Event(eventName:String,eventID:Int,invited: Invited)

object jsonProtocol extends DefaultJsonProtocol {
  implicit val invitedFormat = jsonFormat(Invited,"people Invited")
  implicit val eventFormat = jsonFormat3(Event)
}

object WebServer {

  def main(args:Array[String]): Unit ={

    implicit val system  = ActorSystem()
    implicit val materializer = ActorMaterializer()
    implicit val dispatcher = system.dispatcher
    //println(Event("HelloEvent",2,Array(1,2,3)).toString)
    val route = {
      import jsonProtocol._
      path("Event") {
        post{
          entity(as[Event]) {event =>
            println(event.eventName)
              complete(event)
          }
        }
      }
    }

    val bindingFuture = Http().bindAndHandle(route,"localhost",8080)
    StdIn.readLine() // let it run until user presses return
    bindingFuture
      .flatMap(_.unbind()) // trigger unbinding from the port
      .onComplete(_ => system.terminate()) // and shutdown when done

  }
}

带有
complete(event)
的行给了我一个错误,表示expected
ToResponseMarshallable
,actual event。

要修复将spray json与akka http一起使用时的编组错误,需要将
SprayJsonSupport
混合到
jsonProtocol
对象中

因此,只需添加导入:

import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
换乘路线:

object jsonProtocol extends DefaultJsonProtocol {
致:


PS根据scalastyle,您应该使用
^[A-Z][A-Za-Z]*
命名对象,因此
jsonProtocol

中的第一个字母应该是大写字母。要修复将spray json与akka http一起使用时的编组错误,您需要将
SprayJsonSupport
混合到
jsonProtocol
对象中

因此,只需添加导入:

import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
换乘路线:

object jsonProtocol extends DefaultJsonProtocol {
致:


PS根据scalastyle,您应该使用
^[A-Z][A-Za-Z]*
来命名对象,因此
jsonProtocol

中的第一个字母应该是大写字母。您是否尝试过使用
jsonFormat1(邀请)
而不是
jsonFormat(邀请,“邀请的人”)
?是的。从我看到的情况来看,这两者没有区别。您是否尝试过使用
jsonFormat1(受邀)
而不是
jsonFormat(受邀,“受邀人员”)
?是的。这和我所看到的没有区别。