1

After upgrading to Slick 3.0 and Play! 2.4, I got this pretty dependency injection feature, but I faced with serialization problems. My Application is simple rest server.

This is the exception which I get

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

This is my entity

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)

}

Here is method to get a list of posts

def getPostList: Future[Seq[PostEntity]] = db.run(posts.result)

My controller starts like this

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))
  }

}

I haven't found any information in the Internet regarding this problem. Found questions, but any answers.

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
green-creeper
  • 316
  • 3
  • 15

2 Answers2

0

My guess is to move implicit val PostWrites to companion object Posts or closer to the DI library (don't know Play that much to offer more help).

It happens because of the way DI works in general - first an instance, and then all the goodies are available that are inside the instance.

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
0

I had the exact same problem upgrading my application to Play! 2.4. If you use the slick code generator for your entities you need to create a custom generator like the one in this answer https://stackoverflow.com/a/32070115

Community
  • 1
  • 1
Fabrizio Fortino
  • 1,479
  • 1
  • 14
  • 22