仅当状态代码为200 OK时,喷涂返回Gzip编码消息

时间:2015-08-20 10:51:00

标签: spray

我使用compressResponse关键字声明路线:

(compressResponse(Gzip) & respondWithMediaType(`application/json`)) {
            super.routes
}

ret.status match {

  case ResponseResult.StatSuccess => complete(200, ret.toString)

  case _ => complete(InternalServerError, ret.toString)

}

但是,我发现ret仅在完成200时被编码为Gzip。对于400,500,Spray只返回纯文本。

1 个答案:

答案 0 :(得分:0)

object AllGzip {
  import MessagePredicate._
  val DefaultFilter = (isRequest || responseStatus(ret => ret.isSuccess || ret.isFailure)) && isCompressible
}

object AllEncodedGzip extends Gzip(AllGzip.DefaultFilter) {
  def apply(messageFilter: MessagePredicate) = new Gzip(messageFilter)
}

将Gzip更改为AllEncodedGzip,spray将编码任何类型的响应消息给Gzip