在使用Slick中的circe从JSON中获取数据时,我可以获取没有日期(Timestamp/DateTime)
字段的实体数据。但是,如果我在实体中使用Timestamp
字段,则会出现错误:
[error] /var/www/html/scala-api/src/main/scala/oc/api/http/routes/TestApi.scala:40: could not find implicit value for parameter encoder: io.circe.Encoder[Seq[oc.api.models.UserEntity]]
[error] auth => complete(userDao.getAll().map(_.asJson))
这是我为Slick实体使用CIRCE进行JSON编码所使用的代码。
BaseTable:
abstract class BaseTable[T](tag: Tag, name: String) extends Table[T](tag, name) {
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def createdAt = column[Timestamp]("created_at")
def updatedAt = column[Timestamp]("updated_at")
def deletedAt = column[Timestamp]("deleted_at")
}
基础实体类:
trait BaseEntity {
val id : Long
def isValid : Boolean = true
}
UserEntity: 创建时间生成编码器错误
case class UserEntity(id: Long, email: String, password: String, createdAt: Timestamp) extends BaseEntity
UserEntity: 这个完美地运作了
case class UserEntity(id: Long, email: String, password: String) extends BaseEntity
UserTable(Slick):
object UserTables {
class UserTable(tag : Tag) extends BaseTable[UserEntity](tag, "users") {
def name = column[String]("name")
def password = column[String]("password")
def * = (id, name, password) <> (UserEntity.tupled, UserEntity.unapply)
}
implicit val accountsTableQ : TableQuery[UserTable] = TableQuery[UserTable]
}
我的代码有什么问题吗?非常感谢您的帮助。
object UserTables { class UserTable(tag : Tag) extends BaseTable[UserEntity](tag, "users") { def updated_at = column[Option[String]]("updated_at") def * = (..., updated_at) <> (...) } implicit val TimestampFormat : Encoder[Timestamp] with Decoder[Timestamp] = new Encoder[Timestamp] with Decoder[Timestamp] { override def apply(a: Timestamp): Json = Encoder.encodeLong.apply(a.getTime) override def apply(c: HCursor): Result[Timestamp] = Decoder.decodeLong.map(s => new Timestamp(s)).apply(c) } }
但是出现了编码器错误。 - Sujit Baniya