gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:51:31 26 4
gpt4 key购买 nike

我正在尝试使用 Scala Play 2、Slick 3 和 PostgreSQL 构建网络应用程序。我的目的是从 Controller 返回 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)
}
}

这是我的 Controller -

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

目前我收到“未找到未应用或未应用序列函数”异常。这是完整的堆栈跟踪 -

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]

最佳答案

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

我当前的模型 -

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

和当前 Controller -

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

关于json - 在 Scala Play 应用程序中获取 “No unapply or unapplySeq function found” 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32715450/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com