我是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案例类
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"
)
}
非常感谢。
答案 0 :(得分:0)
请勿将您的隐式定义放入您的类型中。而是创建一个对象并将其放入内部:
object serializers {
implicit def UserDtoCodecJson: CodecJson[UserDto] =
casecodec6(UserDto.apply, UserDto.unapply)(
"id", "username", "email",
"firstName", "lastName", "balance"
)
}
对错误类型执行相同的操作。在路线范围内导入对象。我想Argonaut可以通过隐式解析来创建Either类型。
答案 1 :(得分:0)
如果这是普通的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
也有编组器?
答案 2 :(得分:0)
最后,我实现了一个方法,该方法采用一个export type IFunctionalMapping<I, O> = {
[K in keyof O]/*?*/: (input: I) => O[K]; // If you don't want all properties to be required, uncomment the ?
}
export interface IInput {
firstName: string;
lastName: string;
}
export interface IOutput {
fullName: string;
}
const mapping: IFunctionalMapping<IInput, IOutput> = {
fullName: (i) => `${i.firstName} ${i.lastName}`
}
和一个statusCode
,解析其中的Either[E, R]
并使用所需的值调用complete。
Either