在trait Reads中覆盖方法读取时出错

时间:2014-09-23 14:16:52

标签: mongodb scala playframework-2.0 securesocial

我正在编写播放应用程序2.3(使用Scala)。 我正在使用安全的社交模块(master)。 现在我创建了我的用户类:

class LoginUser(
    providerId: String,
    userId: String,
    firstName: Option[String],
    lastName: Option[String],
    fullName: Option[String],
    email: Option[String],
    avatarUrl: Option[String],
    authMethod: AuthenticationMethod,
    oAuth1Info: Option[OAuth1Info] = None,
    oAuth2Info: Option[OAuth2Info] = None,
    passwordInfo: Option[PasswordInfo] = None
               ) extends BasicProfile(providerId, userId, firstName, lastName, fullName,
                                      email, avatarUrl,authMethod, oAuth1Info, oAuth2Info,
                                      passwordInfo)

对于我的mongodb中的读/写用户日期,我写了一个隐含的对象:

object LoginUser {
  implicit val authenticationMethodFormat = Json.format[AuthenticationMethod]
  implicit val oAuth1InfoFormat = Json.format[OAuth1Info]
  implicit val oAuth2InfoFormat = Json.format[OAuth2Info]
  implicit val passwordInfoFormat = Json.format[PasswordInfo]
  implicit val profileFormat = Json.format[BasicProfile]
  implicit object LoginUserFormat extends Format[LoginUser] {
    def writes(user: LoginUser): JsValue = Json.obj(
        "providerId" -> user.providerId,
        "userId" -> user.userId,
        "firstName" -> user.firstName,
        "lastName" -> user.lastName,
        "fullName" -> user.fullName,
        "email" -> user.email,
        "avatarUrl" -> user.avatarUrl,
        "authMethod" -> user.authMethod,
        "oAuth1Info" -> user.oAuth1Info,
        "oAuth2Info" -> user.oAuth2Info,
        "passwordInfo" -> user.passwordInfo)
   def reads(json: JsValue): LoginUser = new LoginUser(
       (json \ "providerId").as[String],
       (json \ "userId").as[String],
       (json \ "firstName").as[Option[String]],
       (json \ "lastName").as[Option[String]],
       (json \ "fullName").as[Option[String]],
       (json \ "email").as[Option[String]],
       (json \ "avatarUrl").as[Option[String]],
       (json \ "authMethod").as[AuthenticationMethod],
       (json \ "oAuth1Info").as[Option[OAuth1Info]],
       (json \ "oAuth2Info").as[Option[OAuth2Info]],
       (json \ "passswordInfo").as[Option[PasswordInfo]]
       )


  } 

但编译器告诉我以下错误:

[error] /Users/alberto/git/recommendation-system/app/security/UserService.scala:60: overriding method reads in trait Reads of type (json: play.api.libs.json.JsValue)play.api.libs.json.JsResult[security.LoginUser];
[error]  method reads has incompatible type
[error]    def reads(json: JsValue): LoginUser = new LoginUser(
[error]        ^
[error] one error found
[error] (compile:compile) Compilation failed

出了什么问题?

1 个答案:

答案 0 :(得分:1)

overriding method reads in trait Reads of type (json: play.api.libs.json.JsValue)play.api.libs.json.JsResult[security.LoginUser];
[error]  method reads has incompatible type

表示reads特征中Reads方法的方法签名是

def read(json:JsValue) : JsResult[LoginUser]

因为你试图实现

def read(json:JsValue) : LoginUser

您的方法签名与基本特征之一冲突,因此失败。