我正在努力学习下面的例子
https://doc.akka.io/docs/akka-http/current/common/json-support.html
我的代码看起来像
final case class LeaderboardPostRequest(name: String, kind: String)
final case class LeaderboardPostResponse(name: Option[String], id: String)
trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
implicit val requestFormat = jsonFormat2(LeaderboardPostRequest)
implicit val responseFormat = jsonFormat2(LeaderboardPostResponse)
}
class LeaderboardEndpoint extends Directives with JsonSupport {
. . .
def leaderboardPost(name: Option[String]): Route =
post {
logRequest("leaderboard", Logging.InfoLevel) {
entity(as[LeaderboardPostRequest]) { leaderboard =>
try {
complete(leaderboardCreate(Some(leaderboard.name), Some(leaderboard.kind)))
} catch {
case cause: LeaderboardException => complete(cause.getHttpResponse)
case cause: Throwable =>
complete(HttpResponse(InternalServerError, entity = s"Exception thrown from LeaderboardPost: ${cause.getMessage}"))
}
} ~
complete(HttpResponse(BadRequest, entity = "****body missing****"))
}
}
. . .
}
原木看起来像
HttpRequest(HttpMethod(POST),http://localhost:8080/leaderboard?name=foo,List(User-Agent: Mozilla/5.0 (Windows NT; Windows NT 10.0; en-CA) WindowsPowerShell/5.1.17763.134, Host: localhost:8080, Timeout-Access: <function1>),HttpEntity.Strict(application/json,{name="foo",kind="ConcurrentLeaderboard"}),HttpProtocol(HTTP/1.1))
但结果总是如此
****body missing****
我怀疑这是件简单的事情,但我被阻止了,不知道需要什么额外的魔法。如有帮助/建议/提示,将不胜感激。