【发布时间】:2020-08-26 04:45:23
【问题描述】:
我在尝试返回将 Future of Object 作为 json 的操作时收到错误。我的方法是这样的:
def register: Action[JsValue] = Action.async(parse.json) {
implicit request: Request[JsValue] => {
val registerCredentials: RegisterDto = request.body.as[RegisterDto]
if (registerCredentials.password != registerCredentials.repeatPassword) {
NotAcceptable("Passwords don't match")
}
Try.apply(registerService.createUser(registerCredentials.email, registerCredentials.password))
.map(user => Ok(Json.toJson(user)))
.orElse(InternalServerError(s"Error creating new user") )
}
注册服务
registerService.createUser(registerCredentials.email, registerCredentials.password)
返回用户的未来,我想从我的控制器返回它,因为数据库执行是使用 Slick 异步的。
尝试启动服务器后,我收到以下错误:
No Json serializer found for type scala.concurrent.Future[login.register.dao.user.User]. Try to implement an implicit Writes or Format for this type.
[error] .map(user => Ok(Json.toJson(user)))
我的 User 类有一个隐式映射器:
case class User(id: Long, email: String, password: String, isActive: Boolean)
object User {
implicit val personFormat = Json.format[User]
}
我认为将其作为未来返回然后是 Json 存在问题。但它在另一个端点对我有用,我不必使用Action.async(parse.json),只需Action.async
我正在寻找从请求中读取 Json 的解决方案,然后将对象的一些 Future 作为 json 返回。
【问题讨论】:
标签: json scala asynchronous playframework