我正在尝试遵循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****
我怀疑这很简单,但是我被封锁了,无法弄清楚还需要什么额外的魔法。帮助/建议/提示将不胜感激。
答案 0 :(得分:1)
我认为可能存在几个(也许是相交的)问题:
根据我在日志消息中看到的,是路由的path
未配置,请引用path directive docs。
也许您滥用了logRequest
方法。参数值"leaderbord"
只是日志的一个标记,至少logRequest directive docs所说的
我不确定有效载荷本身是否正确:{name="foo",kind="ConcurrentLeaderboard"}
。键名也应该用引号引起来(但也许就像记录的一样,没有引号)。并且,如果akka-http无法将有效负载映射到LeaderboardPostRequest
案例类,则根据compose directive docs采取路由中的下一个选项,BadRequest
是该选项