我有以下方法:
private def initiateAuth(req: LoginRequest, userAgent: String, deviceId: String) = {
extractClientIP { clientIp =>
extractRequestContext { implicit ctx =>
if (clientIp.toOption.isEmpty) reject(Rejections.validationRejection("abc"))
val xForwardedFor = clientIp.toOption.map(_.getHostAddress).get
mapToResponse(userAuthenticator.auth(req.username, req.password, deviceId, xForwardedFor, userAgent))
}
}
}
真正奇怪的是Rejections.validationRejection("abc")
工作,但当我使用Rejections.missingHeader("abc")
我收到一个错误:
这两者有什么不同?我很困惑因为MissingHeaderRejection实现拒绝,所以为什么类型不匹配?
在clientIp选项为空的if语句中,if的结果将被忽略。我最好使用模式匹配或if - else表达式