如何解决 Complete-Akka HTTP 中的 Either[Error, User]
How to resolve an Either[Error, User] in Complete- Akka HTTP
我是 Akka HTTP 的新手,我正在尝试定义下面的端点。
在路由级别我有这个端点:
def login: Route = {
post(
entity(as[UserLogin]) { userLogin =>
complete(
authService.loginUser(userLogin)
)
}
)
}
authService.loginUser
的签名是
def loginUser(userLogin: UserLogin): Either[Error, UserDto]
问题是如何解决路由中的 Either[Error, UserDto]
,因为我现在收到此错误
Type mismatch, expected: ToResponseMarshallable, actual: Either[Error, UserDto]
UserDto 案例class
case class UserDto(id: Int,
username: String,
email: String,
firstName: String,
lastName: String,
balance: BigDecimal) {
implicit def UserDtoCodecJson: CodecJson[UserDto] =
casecodec6(UserDto.apply, UserDto.unapply)(
"id", "username", "email",
"firstName", "lastName", "balance"
)
}
非常感谢。
不要将你的隐式定义放在你的类型中。而是创建一个对象并将其放入:
object serializers {
implicit def UserDtoCodecJson: CodecJson[UserDto] =
casecodec6(UserDto.apply, UserDto.unapply)(
"id", "username", "email",
"firstName", "lastName", "balance"
)
}
对错误类型做同样的事情。在路由范围内导入对象。我想 Argonaut 可以通过隐式解析创建 Either 类型。
如果这是一个普通的 Scala Either
,akka-http
是 providing an implicit conversion 为它生成一个编组器,假设您已经为各个类型准备了编组器:
implicit def eitherMarshaller[A1, A2, B](implicit m1: Marshaller[A1, B], m2: Marshaller[A2, B]): Marshaller[Either[A1, A2], B]
您是否检查过您是否也有可用于 Error
的编组器?
我最终做了什么,我实现了一个采用 statusCode
和 Either[E, R]
的方法,解析内部的 Either
并使用所需的值调用完成。
def completeEither[E <: ServiceError, R: ToEntityMarshaller]
(statusCode: StatusCode, either: => Either[E, R])(
implicit mapper: ErrorMapper[E, HttpError]
): Route = {
either match {
case Left(value) => complete(statusCode, ErrorResponse(code = value.code, message = value.message))
case Right(value) => complete(value)
}
}
我是 Akka HTTP 的新手,我正在尝试定义下面的端点。
在路由级别我有这个端点:
def login: Route = {
post(
entity(as[UserLogin]) { userLogin =>
complete(
authService.loginUser(userLogin)
)
}
)
}
authService.loginUser
的签名是
def loginUser(userLogin: UserLogin): Either[Error, UserDto]
问题是如何解决路由中的 Either[Error, UserDto]
,因为我现在收到此错误
Type mismatch, expected: ToResponseMarshallable, actual: Either[Error, UserDto]
UserDto 案例class
case class UserDto(id: Int,
username: String,
email: String,
firstName: String,
lastName: String,
balance: BigDecimal) {
implicit def UserDtoCodecJson: CodecJson[UserDto] =
casecodec6(UserDto.apply, UserDto.unapply)(
"id", "username", "email",
"firstName", "lastName", "balance"
)
}
非常感谢。
不要将你的隐式定义放在你的类型中。而是创建一个对象并将其放入:
object serializers {
implicit def UserDtoCodecJson: CodecJson[UserDto] =
casecodec6(UserDto.apply, UserDto.unapply)(
"id", "username", "email",
"firstName", "lastName", "balance"
)
}
对错误类型做同样的事情。在路由范围内导入对象。我想 Argonaut 可以通过隐式解析创建 Either 类型。
如果这是一个普通的 Scala Either
,akka-http
是 providing an implicit conversion 为它生成一个编组器,假设您已经为各个类型准备了编组器:
implicit def eitherMarshaller[A1, A2, B](implicit m1: Marshaller[A1, B], m2: Marshaller[A2, B]): Marshaller[Either[A1, A2], B]
您是否检查过您是否也有可用于 Error
的编组器?
我最终做了什么,我实现了一个采用 statusCode
和 Either[E, R]
的方法,解析内部的 Either
并使用所需的值调用完成。
def completeEither[E <: ServiceError, R: ToEntityMarshaller]
(statusCode: StatusCode, either: => Either[E, R])(
implicit mapper: ErrorMapper[E, HttpError]
): Route = {
either match {
case Left(value) => complete(statusCode, ErrorResponse(code = value.code, message = value.message))
case Right(value) => complete(value)
}
}