如何将密封的特征解码为JSON-Circe

时间:2018-10-25 17:24:03

标签: akka akka-http circe

我下面有密封的特质和它的case类,我想将其转换为JSON以作为我的Akka Http应用程序中的响应。

sealed trait HttpRestError {
  val statusCode: StatusCode
  val code: String
  val message: String
}

case class UnauthorizedError() extends HttpRestError {
  override val statusCode: StatusCode = Unauthorized
  override val code: String = "Unauthorized"
  override val message: String = "Unauthorized"
}

路线部分

def login2: Route = {
  path("test") {
    pathEndOrSingleSlash {
      get {
        onComplete(authService.testUser.toRestError[UnauthorizedError]) {
          case util.Success(f) => f match {
            case Left(error) =>
              complete((error.statusCode, error))
            case Right(v) => complete(OK -> v)
          }
          case util.Failure(ex) =>
            complete(StatusCodes.InternalServerError)
        }
      }
    }
  }
}

问题在于,当Either返回Left时,响应为空,但错误代码正确。 有想法吗?

0 个答案:

没有答案