我需要用akka-http和reactivemongo编写简单的Web服务。
保存数据的功能如下所示
def saveRoute(route: Route):Future[WriteResult] = {
collection.insert(route)
}
调用此函数的代码如下所示
val userRoutes = {
logRequestResult("akka-http-microservice") {
path("routes") {
(post & entity(as[Route])) { route =>
Database.saveRoute(route)
}
}
}
}
我需要返回带有插入ID的Route的结果,并且不要让线程等待这样做。 如果试试
Database.saveRoute(route).onComplete{
case Success(r) => complete(r.toString)
case Failure(e) => complete(e.getMessage)
}
它无法编译,因为它没有返回值。 我知道如何以肮脏的方式制作它,但真的想以适当的方式制作。
在这种情况下该怎么做?
答案 0 :(得分:5)
似乎我找到了最有效的方法。它内置于onComplete指令
(path("routes" / "add") & post & entity(as[Route])) {
route =>
onComplete(routesController.addRoute(route)) {
case Success(result) => complete(StatusCodes.Created, "OK")
case Failure(ex) => complete(new ErrorResponse(StatusCodes.InternalServerError.intValue, ErrorResponse.ERROR, ex.getMessage))
}
}
答案 1 :(得分:3)
使用onSuccess
处理未来完成时的有效响应,handleExceptions
处理未来未成功时的处理。
val userRoutes = {
handleExceptions(mongoDbExceptionHandler) {
logRequestResult("akka-http-microservice") {
path("routes") {
(post & entity(as[Route])) { route =>
onSuccess(Database.saveRoute(route)) { result =>
complete(result)
}
}
}
}
}
}
// Something like this for whatever the exceptions you expect are
val mongoDbExceptionHandler = ExceptionHandler {
case ex: MongoDbReadException => complete(HttpResponse(InternalServerError, "No database")))
}
的onSuccess: http://doc.akka.io/docs/akka/2.4.9/scala/http/routing-dsl/directives/future-directives/onSuccess.html
handleExceptions: http://doc.akka.io/docs/akka/2.4.9/scala/http/routing-dsl/exception-handling.html
答案 2 :(得分:1)
您可以映射未来,然后完成下面的请求。
val future = Database.saveRoute(route)
val response = future.map(_.getId).recover(_.getMessage)
complete(response)
另一方面,为了处理异常,最好使用ExceptionHandler并将其与路径包装在一起。您可以找到示例here。
答案 3 :(得分:1)
您几乎没有选择,我会尝试将最常用的选项用于基于REST API的解决方案:
当您希望期望由期望处理器起泡并处理时,使用OnSuccess
concat(
path("success") {
onSuccess(Future { "Ok" }) { extraction =>
complete(extraction)
}
},
path("failure") {
onSuccess(Future.failed[String](TestException)) { extraction =>
complete(extraction)
}
}
)
https://doc.akka.io/docs/akka-http/current/routing-dsl/directives/future-directives/onSuccess.html
onComplete:要手动处理异常时。尝试包裹Monad。
val route =
path("divide" / IntNumber / IntNumber) { (a, b) =>
onComplete(divide(a, b)) {
case Success(value) => complete(s"The result was $value")
case Failure(ex) => complete((InternalServerError, s"An error occurred: ${ex.getMessage}"))
}
}
https://doc.akka.io/docs/akka-http/current/routing-dsl/directives/future-directives/onComplete.html
答案 4 :(得分:0)
怎么样,替换:
Database.saveRoute(route)
使用:
complete(Database.saveRoute(route).map(_.toString).recover(_.getMessage))