0

MongoController 提供 serve 函数来提供查询结果(作为光标)。我只想做一些不同于让服务返回 NotFound 的事情,比如发送一些其他默认文件。我想知道是否可以使用模式匹配来检查结果。签名是这样的:

/** Returns a future Result that serves the first matched file, or NotFound. */
  def serve[T <: ReadFile[_ <: BSONValue], Structure, Reader[_], Writer[_]](gfs: GridFS[Structure, Reader, Writer], foundFile: Cursor[T], dispositionMode: String = CONTENT_DISPOSITION_ATTACHMENT)(implicit ec: ExecutionContext): Future[SimpleResult] = {
4

2 回答 2

1

在您的操作功能中,您可以执行以下操作:

serve(...).flatMap(serveSuccess => aCustomFutureRes).recoverWith { case error => aFutureResOnFailure }
于 2015-06-17T07:27:56.367 回答
0

最后,找到了这个解决方案:

serve(
  gridFS, 
  gridFS.find(query),
  CONTENT_DISPOSITION_INLINE)
.flatMap{ result => 
  result match {
    case NotFound => // Handle file not found
    case _ => Future.successful(result)
  }
}
于 2015-06-18T00:16:11.270 回答