spray.io调试指令 - 将拒绝转换为StatusCodes

时间:2015-07-29 12:26:34

标签: scala akka spray

我正在使用logRequestResponse调试指令来记录通过整个路径树失败的每个请求/响应。日志条目如下所示:

2015-07-29 14:03:13,643 [INFO ] [DataImportServices-akka.actor.default-dispatcher-6] [akka.actor.ActorSystemImpl]ActorSystem(DataImportServices) - get-userr: Response for
  Request : HttpRequest(POST,https://localhost:8080/city/v1/transaction/1234,List(Accept-Language: cs, Accept-Encoding: gzip,...
  Response: Rejected(List(MalformedRequestContentRejection(Protocol message tag had invalid wire type.,...

我的根路线特征,它将所有部分路线组合成一个外观,如下所示:

trait RestAPI extends Directives {
  this: ServiceActors with Core =>

  private implicit val _ = system.dispatcher

  val route: Route =
      logRequestResponse("log-activity", Logging.InfoLevel) {
        new CountryImportServiceApi().route ~
        new CityImportServiceApi().route
  }
}

部分路线定义如下:

class CinemaImportServiceApi()(implicit executionContext: ExecutionContext) extends Directives {

  implicit val timeout = Timeout(15 seconds)    

  val route: Route = {
    pathPrefix("city") {
      pathPrefix("v1") {
        path("transaction" / Segment ) {
          (siteId: String, transactionId: String) =>
            post {
              authenticate(BasicAuth(cityUserPasswordAuthenticator _, realm = "bd city import api")) {
                user =>
                  entity(as[CityTrans]) { e =>
                    complete {
                      StatusCodes.OK

                    }
                  }
              }
            }
        }
      }
    }
  }

汇编的路由通过HttpServiceActor runRoute运行。

我想将拒绝转换为StatusCode并通过logRequestResponse记录。即使我为记录编写自定义函数,我也会遭到拒绝。在我看来可疑的是,因为它被包裹整个路由树拒绝仍然没有转换为HttpResponse。在测试中,我们密封路线以将拒绝转换为HttpResponse。有没有办法如何将路线标记为完整的路线,从而实际上密封它?或者我在这里错过了一些重要的概念?

THX

1 个答案:

答案 0 :(得分:0)

我会添加如下内容:

} ~ // This is the closing brace of the pathPrefix("city"), just add the ~
pathPrefix("") {
  complete {
    (StatusCodes.OK, "Invalid Route")
  }
}

当然,将OK和身体改为你需要的任何东西。 pathPrefix("")将匹配任何先前路由拒绝的任何路径。