如何以编程方式执行拒绝处理程序的Route并获取结果HttpEntity? [英] How can I programmatically execute Route of rejection handler and get resulting HttpEntity?

查看:106
本文介绍了如何以编程方式执行拒绝处理程序的Route并获取结果HttpEntity?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式执行拒绝处理程序的 Route 并得到结果 HttpEntity

How can I programmatically execute Route of rejection handler and get resulting HttpEntity ?

例如,假设我有 RequestContext 对象和 Rejection 对象在其上执行 RejectionHandler.default 并获得 HttpEntity

For example assuming that I have RequestContext object and Rejection object I'd like to execute RejectionHandler.default on it and get HttpEntity.

这是我要执行的操作的示例:

Here is example of what I'd like to do:

implicit def myRejectionHandler =
  RejectionHandler.newBuilder()
    .handleAll[Rejection] { rejections ⇒

    def prefixEntity(entity: ResponseEntity): ResponseEntity = entity match {
      case HttpEntity.Strict(contentType, data) => {
        import spray.json._
        val text = ErrorResponse(0, "Rejection", data.utf8String).toJson.prettyPrint
        HttpEntity(ContentTypes.`application/json`, text)
      }
      case _ =>
        throw new IllegalStateException("Unexpected entity type")
    }

    val route: Route = extractRequestContext { ctx =>
      mapResponseEntity(prefixEntity) {
        // Here I want result of `complete` route from RejectionHandler.default
      }
    }
    route
  }
    .handleNotFound {
      complete((NotFound, "Not here!"))
    }
    .result()


推荐答案

我想我明白了您想要的内容。您可以仅在注释所在的位置应用默认拒绝处理程序。唯一的是, apply 会返回一个 Option ,而该 None 如果遇到的拒绝在该拒绝处理程序中没有作用。鉴于您使用的是默认处理程序,而且几乎可以处理所有内容,所以这种情况不太可能发生,但是您仍然需要在代码中加以说明(因此,我的 getOrElse 会导致通用 InternalServerError )。修改后的代码如下所示:

I think I get the gist of what you want. You can just apply the default rejection handler in there where your comment is. The only thing is that the apply there returns an Option that will be None if the rejections encountered don't hit on anything in that rejection handler. This is unlikely given that you are using the default handler and it pretty much handles everything, but you still need to account for it in the code (hence my getOrElse that results in a generic InternalServerError). The modified code would look like this:

val defaultRejectionHandler = RejectionHandler.default

implicit def myRejectionHandler =
  RejectionHandler.newBuilder()
    .handleAll[Rejection] { rejections ⇒

    def prefixEntity(entity: ResponseEntity): ResponseEntity = entity match {
      case HttpEntity.Strict(contentType, data) => {
        import spray.json._
        val text = ErrorResponse(0, "Rejection", data.utf8String).toJson.prettyPrint
        HttpEntity(ContentTypes.`application/json`, text)
      }
      case _ =>
        throw new IllegalStateException("Unexpected entity type")
    }

    val route: Route = extractRequestContext { ctx =>
      mapResponseEntity(prefixEntity) {
        defaultRejectionHandler.apply(rejections).getOrElse{
          complete(StatusCodes.InternalServerError)
        }
      }
    }
    route
  }
  .handleNotFound {
    complete((NotFound, "Not here!"))
  }
  .result()

这可以编译,但是我没有实际测试它是否可以达到预期的效果。另外,我不需要 RequestContext ctx ,因此您可以删除该 extractRequestContext 层。

This compiles, but I did not actually test it to see if we get the desired effect. Also, I didn't need the RequestContext value ctx, so you might be able to remove that extractRequestContext layer in there.

这篇关于如何以编程方式执行拒绝处理程序的Route并获取结果HttpEntity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆