Akka HTTP 路由和封送处理

Akka HTTP Routing and Marshaling

我刚开始使用 Akka HTTP,在路由 DSL 和封送处理方面遇到了一些麻烦。 'route' 设置中的波浪号导致错误:

value ~ is not a member of akka.http.scaladsl.server.RequestContext ⇒ scala.concurrent.Future[akka.http.scaladsl.server.RouteResult] possible cause: maybe a semicolon is missing before 'value ~'?

此外,'get' 子句中的 JSON 编组导致错误:

◾Cannot find JsonWriter or JsonFormat type class for scala.collection.immutable.Map[String,scala.collection.mutable.Map[String,Tweet]]

◾not enough arguments for method toJson: (implicit writer: spray.json.JsonWriter[scala.collection.immutable.Map[String,scala.collection> .mutable.Map[String,Tweet]]])spray.json.JsValue. Unspecified value parameter writer.

我已经非常仔细地遵循了文档示例,所以如果您能帮助我理解这些错误以及如何解决它们,我将不胜感激。谢谢。

API

import akka.actor.ActorSystem
import scala.concurrent.Future
import akka.stream.ActorMaterializer
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives.path
import akka.http.scaladsl.server.Directives.pathPrefix
import akka.http.scaladsl.server.Directives.post
import akka.http.scaladsl.server.Directives.get
import akka.http.scaladsl.server.Directives.complete
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import akka.http.scaladsl.server.Directives.{entity, as}
import akka.http.scaladsl.model.StatusCodes.{Created, OK}
import spray.json._
import DefaultJsonProtocol._
import akka.stream.Materializer
import scala.concurrent.ExecutionContext

trait RestApi {
  import TweetProtocol._
  import TweetDb._

  implicit val system: ActorSystem
  implicit val materializer: Materializer
  implicit val execCtx: ExecutionContext

  val route =
    pathPrefix("tweets") {
      (post & entity(as[Tweet])) { tweet =>
        complete {
          Created -> Map("id" -> TweetDb.save(tweet)).toJson
        }
      } ~ 
      (get) {
        complete {
          OK -> Map("tweets" -> TweetDb.find()).toJson
        }
      } 
    }   
}

object TweetApi extends App with RestApi {
  implicit val system = ActorSystem("webapi")
  implicit val materializer = ActorMaterializer()
  implicit val execCtx = system.dispatcher

  val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)

  println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
  Console.readLine()

  bindingFuture.flatMap(_.unbind()).onComplete { _ => system.shutdown() }
}

协议

import spray.json.DefaultJsonProtocol

case class Tweet(author: String, body: String)

object TweetProtocol extends DefaultJsonProtocol {
  implicit val TweetFormat = jsonFormat2(Tweet.apply)
}

伪数据库

import scala.collection.mutable.Map

object TweetDb {
  private var tweets = Map[String, Tweet]()

  def save(tweet: Tweet) = {
    val id: String = java.util.UUID.randomUUID().toString()
    tweets += (id -> tweet)
    id
  }

  def find() = tweets

  def findById(id: String) = tweets.get(id)
}

对于第一个错误,请尝试评论中的建议,即。全部从指令导入

第二部分

◾Cannot find JsonWriter or JsonFormat type class for scala.collection.immutable.Map[String,scala.collection.mutable.Map[String,Tweet]]

◾not enough arguments for method toJson: (implicit writer: spray.json.JsonWriter[scala.collection.immutable.Map[String,scala.collection> .mutable.Map[String,Tweet]]])spray.json.JsValue. Unspecified value parameter writer.

您需要为 Map[String, mutable.Map[String, Tweet]]

定义 JsonFormat

通过在你的 TweetProtocol 中创建一个对象,扩展 RootJsonFormat 例如

type DBEntry = Map[String, mutable.Map[String, Tweet]]
object TweetProtocol extends DefaultJsonProtocol {
  implicit object DBEntryJsonFormat extends RootJsonFormat[DBEntry] {
    override def read(json: JSValue) { 
      // your implementation
    }
    override def write(dbEntry: DBEntry) {
      // implementation
    }
  }
}