在Scala Play应用程序中获取“No unapply或unapplySeq function found”异常

时间:2015-09-22 11:15:58

标签: json scala playframework

我正在尝试使用Scala Play 2,Slick 3和PostgreSQL的Web应用程序。我的目的是从控制器返回JSON。

这些是依赖关系 -

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
)

这是我的模特 -

package models

import play.api.libs.json.Json
import slick.driver.PostgresDriver.api._
import scala.concurrent.Future

case class User(id: String, name: String)

object Users extends ModelInit {
    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)
    }

    lazy val users = TableQuery[UserModel]
    implicit val userFormat = Json.format[UserModel]

    def all = {
        db.run(users.result)
    }

    def create(user: User): Future[Int] = {
        db.run(users += user)
    }

    def getById(name: String) = {
        val selectQuery = users.map(_.id)
        val selectAction = selectQuery.result
        db.run(selectAction)
   }
}

这是我的控制器 -

package controllers

import models.Users
import play.api.mvc._
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.json._

class Application extends Controller {
    def index = Action {
        Users.all.map(user => println(Json.toJson(user)))
        Ok(views.html.index("Your new application is ready."))
    }
}

目前我正在获取&#34;未找到unapply或unapplySeq函数&#34;例外。这是完整的堆栈跟踪 -

play.sbt.PlayExceptions$CompilationException: Compilation error[No unapply or unapplySeq function found]
        at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:27) ~[na:na]
        at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:27) ~[na:na]
        at scala.Option.map(Option.scala:145) ~[scala-library.jar:na]
        at play.sbt.run.PlayReload$$anonfun$taskFailureHandler$1.apply(PlayReload.scala:49) ~[na:na]
        at play.sbt.run.PlayReload$$anonfun$taskFailureHandler$1.apply(PlayReload.scala:44) ~[na:na]
        at scala.Option.map(Option.scala:145) ~[scala-library.jar:na]
        at play.sbt.run.PlayReload$.taskFailureHandler(PlayReload.scala:44) ~[na:na]
        at play.sbt.run.PlayReload$.compileFailure(PlayReload.scala:40) ~[na:na]
        at play.sbt.run.PlayReload$$anonfun$compile$1.apply(PlayReload.scala:17) ~[na:na]
        at play.sbt.run.PlayReload$$anonfun$compile$1.apply(PlayReload.scala:17) ~[na:na]
        at scala.util.Either$LeftProjection.map(Either.scala:377) ~[scala-library.jar:na]
        at play.sbt.run.PlayReload$.compile(PlayReload.scala:17) ~[na:na]
        at play.sbt.run.PlayRun$$anonfun$playRunTask$1$$anonfun$apply$2$$anonfun$apply$3$$anonfun$2.apply(PlayRun.scala:61) ~[na:na]
        at play.sbt.run.PlayRun$$anonfun$playRunTask$1$$anonfun$apply$2$$anonfun$apply$3$$anonfun$2.apply(PlayRun.scala:61) ~[na:na]
        at play.runsupport.Reloader$$anonfun$reload$1.apply(Reloader.scala:323) ~[na:na]
        at play.runsupport.Reloader$$anon$3.run(Reloader.scala:43) ~[na:na]
        at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_05]
        at play.runsupport.Reloader$.play$runsupport$Reloader$$withReloaderContextClassLoader(Reloader.scala:39) ~[na:na]
        at play.runsupport.Reloader.reload(Reloader.scala:321) ~[na:na]
        at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1.apply(DevServerStart.scala:113) ~[play-server_2.11-2.4.3.jar:2.4.3]
        at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1.apply(DevServerStart.scala:111) ~[play-server_2.11-2.4.3.jar:2.4.3]
        at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24) ~[scala-library.jar:na]
        at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) ~[scala-library.jar:na]
        at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40) ~[akka-actor_2.11-2.3.13.jar:na]
        at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397) ~[akka-actor_2.11-2.3.13.jar:na]
        at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) ~[scala-library.jar:na]
        at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) ~[scala-library.jar:na]
        at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) ~[scala-library.jar:na]
        at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) ~[scala-library.jar:na]

1 个答案:

答案 0 :(得分:1)

将Json格式化程序移动到控制器解决了这个问题。

我目前的模特 -

package models

import play.api.libs.json.Json
import slick.driver.PostgresDriver.api._
import scala.concurrent.Future

case class User(id: String, name: String)

object Users extends ModelInit {
    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)
    }

    lazy val users = TableQuery[UserModel]

    def all = {
        db.run(users.result)
    }

    def create(user: User): Future[Int] = {
        db.run(users += user)
    }

    def getById(name: String) = {
        val selectQuery = users.map(_.id)
        val selectAction = selectQuery.result
        db.run(selectAction)
   }
}

目前的控制器 -

package controllers

import models.Users
import play.api.mvc._
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.json._

class Application extends Controller {
    def index = Action {
        implicit val userFormat = Json.format[UserModel]
        Users.all.map(user => println(Json.toJson(user)))
        Ok(views.html.index("Your new application is ready."))
    }
}