我下面有密封的特质和它的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
时,响应为空,但错误代码正确。
有想法吗?