升级到 Slick 3.0 和 Play 后!2.4,我得到了这个漂亮的依赖注入功能,但我遇到了序列化问题。我的应用程序是简单的休息服务器。
这是我得到的例外
type mismatch;
found : play.api.libs.json.OWrites[ReportsDatabase.this.PostEntity]
required: play.api.libs.json.Writes[ApiRoot.this.noiseModel.PostEntity]
Note: implicit value PostWrites is not applicable here because it comes after the application point and it lacks an explicit result type
这是我的实体
val posts = TableQuery[Posts]
case class PostEntity(id: Long, user: Long, text: String, date: LocalDate, lat: Double, lon: Double, pictureID: Long, soundId: Long)
class Posts(tag: Tag) extends Table[PostEntity](tag, "post") {
implicit val dateColumnType = MappedColumnType.base[LocalDate, String](dateFormatter.print(_), dateFormatter.parseLocalDate)
def id = column[Long]("id", O.AutoInc, O.PrimaryKey)
def userId = column[Long]("userId")
def text = column[String]("text")
def date = column[LocalDate]("date_post")
def lat = column[Double]("lat")
def lon = column[Double]("lon")
def pictureId = column[Long]("pictureID")
def soundId = column[Long]("soundId")
def * = (id, userId, text, date, lat, lon, pictureId, soundId) <>(PostEntity.tupled, PostEntity.unapply)
def user = foreignKey("post_user_FK", userId, users)(_.id)
}
这是获取帖子列表的方法
def getPostList: Future[Seq[PostEntity]] = db.run(posts.result)
我的控制器是这样开始的
class ApiRoot @Inject() (noiseDao: NoiseModel, noiseModel: ReportsDatabase) extends Controller {
import noiseModel._
implicit val PostWrites = Json.writes[noiseModel.PostEntity]
def getPostStream = Action.async { implicit request =>
noiseDao.getPostList.map{posts =>
Ok(toJson(posts))
}
}
def getPost(id: Long) = Action.async { implicit request =>
noiseDao.getPost(id).map{ post =>
Ok(toJson(post))
}
}
我在 Internet 上没有找到任何有关此问题的信息。发现问题,但任何答案。