我试图使用play和scala创建一个简单的restful api。我已经能够创建一些基本路由,让它发回json响应,并验证在POST中发送的json。现在我想在postgres数据库中添加持久性,这是它开始崩溃的地方。
我首先将db信息添加到我的application.conf中,创建了一个演变来创建一个表。然后我尝试点击我的GET端点以查看它是否运行了演变,但我反而得到了一个错误。这是html的错误输出(我使用我的浏览器命中端点):
CreationException: Unable to create injector, see the following errors:
1) A binding to play.api.db.DBApi was already configured at play.api.db.DBModule.bindings(DBModule.scala:25):
Binding(interface play.api.db.DBApi to ProviderConstructionTarget(class play.api.db.DBApiProvider)) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1).
at play.api.db.slick.evolutions.EvolutionsModule.bindings(EvolutionsModule.scala:15):
Binding(interface play.api.db.DBApi to ConstructionTarget(class play.api.db.slick.evolutions.internal.DBApiAdapter) in interface javax.inject.Singleton) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)
com.google.inject.CreationException: Unable to create injector, see the following errors:
com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:466)
com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorCreator.java:155)
com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:107)
com.google.inject.Guice.createInjector(Guice.java:96)
com.google.inject.Guice.createInjector(Guice.java:84)
play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:181)
play.api.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.scala:123)
play.api.inject.guice.GuiceApplicationLoader.load(GuiceApplicationLoader.scala:21)
play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1$$anonfun$2.apply(DevServerStart.scala:168)
play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1$$anonfun$2.apply(DevServerStart.scala:164)
play.utils.Threads$.withContextClassLoader(Threads.scala:21)
play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:164)
play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:131)
scala.Option.map(Option.scala:146)
play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1.apply(DevServerStart.scala:131)
play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1.apply(DevServerStart.scala:129)
scala.util.Success.flatMap(Try.scala:231)
play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1.apply(DevServerStart.scala:129)
play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1.apply(DevServerStart.scala:121)
scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1423)
java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:902)
java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1689)
java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1644)
java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
这是我的路线(请注意我实际上并没有尝试连接到数据库):
GET /cities controllers.CityController.index
连接到此控制器方法:
def index = Action {
Ok(Json.obj("status" ->"OK", "message" -> "You did the thing!"))
}
这是我的application.conf,为简洁起见删除了注释:
akka {
}
play.crypto.secret = "changeme"
play.modules {
}
play.i18n {
langs = [ "en" ]
}
play.http {
session {
}
flash {
}
}
play.server.netty {
}
play.ws {
ssl {
}
}
play.cache {
}
play.filters {
cors {
}
csrf {
}
headers {
}
hosts {
}
}
play.evolutions {
}
play.db {
prototype {
}
}
db {
db.default.driver=org.postgresql.Driver
db.default.jdbcUrl="jdbc:postgresql://localhost:5432/transit_service"
db.default.username="redacted"
db.default.password="redacted"
db.default.logSql=true
}
我的演变是一个相当简单的创建表语句,它位于conf / evolutions / default / 1.sql下。
我很感激能够理解为什么会出现这种错误,以及如何让这种错误得以实现。
答案 0 :(得分:0)
Matthias Eckhart在评论中指出:从build.sbt中的libraryDependencies中删除jdbc条目是解决问题的方法。