Getting "No Json serializer found for type scala.concurrent.Future..." exception in Scala Play application -
i trying web application scala play 2, slick 3 , postgresql. purpose return json controller.
these dependencies -
librarydependencies ++= seq( "org.postgresql" % "postgresql" % "9.4-1201-jdbc41", "com.typesafe.slick" %% "slick" % "3.0.0", "com.typesafe.play" %% "play-slick" % "1.0.1", "com.typesafe.play" %% "play-slick-evolutions" % "1.0.1", specs2 % test )
this 1 of model -
package models import play.api.libs.json.json import slick.driver.postgresdriver.api._ import play.api.mvc._ import play.api.libs.json._ import play.api.libs.functional.syntax._ case class user (id: string, name: string) class usermodel (tag: tag) extends table[user](tag, "users") { def id = column[string]("id", o.primarykey) def email = column[string]("email") def * = (id, email) <> (user.tupled, user.unapply _) } object users extends modelinit { lazy val users = tablequery[usermodel] implicit val userformat = json.format[user] def = { db.run(users.result) } }
this controller -
package controllers import models.users import play.api.libs.json.json import play.api.mvc._ class application extends controller { def index = action { val users = users.all val json = json.tojson(users) println(json) ok(views.html.index("your new application ready.")) } }
currently getting following exception-
no json serializer found type scala.concurrent.future[seq[models.usermodel#tableelementtype]]. try implement implicit writes or format type. class application extends controller { 8 def index = action { 9 val users = users.all 10 val json = json.tojson(users) 11 println(json) 12 ok(views.html.index("your new application ready.")) 13 } 14}
you need :
class application extends controller { def index = action { val users = users.all users.map(js => println(json.tojson(js))) // give future[unit] instead of printing can future[json], i.e can map 1 future future. ok(views.html.index("your new application ready.")) } }
or
you can await on future (not recommended) , serialize it.
import concurrent.duration._ val users = await.result(users.all,5.seconds) val json = json.tojson(users) println(json)
ps: have not tested seems solution.
Comments
Post a Comment