Spray send xls file to client

2019-08-30 12:19发布

I'm developing a rest api with spray I need to download from my web-client an excel file with a report.

The excel-generator method is ready but spray's "getFromFile(fileFullPath)" is getting "Internal server error"

Any ideas?

Here is my spray code:

(ctx: RequestContext) => {
   val actor = actorRefFactory.actorOf(Props(new Actor {
      def receive = {
        case GetAnualReport(year, generateExcel) =>
          val flujoActor = context.actorOf(Props[FlujoActor])
          flujoActor ! GetAnualReport(year, generateExcel)
        case ReporteResponse(path) =>
          println("FILE: "+path)
          getFromFile(path)
      }
   }))
actor ! GetAnualReport(year, true)
}

OUTPUT:

FILE: /tmp/flujocaja-reports-5627299217173924055/reporte-anual.xls
HTTP/1.1 500 Internal Server Error

1条回答
老娘就宠你
2楼-- · 2019-08-30 12:42

The main problem with your code is that getFromFile(path) doesn't do anything with the request but instead returns a new function RequestContext => Unit which is never called. One solution could be to replace that line with getFromFile(path)(ctx).

However, there's a better way how to deal asynchronous work before continuing with an inner route: use futures and one of the FutureDirectives. Here's an example roughly adapted to your use case:

onSuccess((flujoActor ? GetAnualReport(year, generateExcel)).mapTo[ReporteResponse]) { response =>
  getFromResource(response.path)
}

That said, I'm not sure why you get 500 Internal Server Error in your scenario. Is there nothing on the console hinting at what the problem is?

查看更多
登录 后发表回答