scala – 如何使用Akka-Http一般地包装拒绝
我想使用Akka Http路由系统及其拒绝系统,但需要将响应Json嵌套在一个通用的Json消息块中.
我以非常非通用的方式工作,创建一个RejectionHandler,然后为所有可能的拒绝添加案例,并使用特定的响应代码和消息处理它们. 例: // Wraps string into control block format def WrappingBlock(msg: String) = ??? val myRejectionHandler = RejectionHandler .newBuilder() .handle{case MalformedRequestContentRejection(msg,detail) => complete(BadRequest,WrappingBlock(msg)) } ... // Further lines for all other possible rejections ... // along with their response codes and messages. ... // It would be nice if this was just generic code ... // rather than specific to every rejection type. .result() val routes = handleRejections(myRejectionHandler){ ... } 但是,我想要的是Akka HTTP默认提供的响应代码以及提供的漂亮的打印消息,只是嵌套在Json控件包装器中,没有针对每种可能的拒绝类型的行.这似乎应该是可能的,但我无法完成它. 解决方法
我认为使用mapRejection与mapResponse显式组合可以做你想做的事.首先,考虑这个简单的路由定义:
(get & path("foo")){ complete((StatusCodes.OK,HttpEntity(ContentTypes.`application/json`,"""{"foo": "bar"}""" ))) } 如果我得到匹配的请求,我将使用json进行响应,我的调用者很高兴,因为他们可以将响应解析为json.但是如果您尝试使用POST请求调用此端点,您将得到如下响应: HTTP 405 Method Not Allowed Date: Wed,06 Jan 2016 13:19:27 GMT Content-Type: text/plain; charset=UTF-8 Content-Length: 47 Allow: GET Server: akka-http/2.3.12 HTTP method not allowed,supported methods: GET 所以在这里我们得到一个不可取的纯文本响应.我们可以通过在路由树的顶部添加一些指令来普遍解决这个问题,如下所示: mapResponse(wrapToJson){ handleRejections(RejectionHandler.default){ (get & path("foo")){ complete((StatusCodes.OK,"""{"foo": "bar"}""" ))) } } } 将wrapToJson定义为: def wrapToJson(resp:HttpResponse):HttpResponse = { //If we get a text/plain response entity,remap it otherwise do nothing val newResp = resp.entity match{ case HttpEntity.Strict(ContentTypes.`text/plain(UTF-8)`,content ) => val jsonResp = s"""{"error": "${content.utf8String}"}""" resp.copy(entity = HttpEntity(ContentTypes.`application/json`,jsonResp)) case other => resp } newResp } 这是一个非常基本的示例,您可能有更好的方法来生成json,但这只是为了说明如何从默认拒绝处理程序修复计划文本响应.现在,您必须显式地在mapResponse下嵌套默认拒绝处理程序,因为自动处理会添加到您定义的任何树的顶层之外,因此mapResponse不会看到拒绝情况.您仍然可以通过RejectionHandler.default获得默认处理. 希望这接近你所追求的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |