我正在使用MongoDB作为数据存储的Java Play 2.1-RC2应用程序。我正在使用Jongo模块来处理MongoDB。 一切顺利,直到我遇到这些错误:
java.lang.NoSuchFieldError:Home
这些错误源自我的模板,是由使用反向路由引起的。在这种特殊情况下,我正试图联系我的家庭控制器。
反向路线如下: @ routes.Home.edit(document.url)
和路线本身是这样的: GET / homepage /:url / edit controllers.Home.edit(url)
之前有效,但自从我开始研究MongoDB实现以来,已经开始提供这些错误。我无法弄清楚两者之间的联系。我认为我不会以任何方式干扰路由。 该项目始于2.0.4项目。迁移到2.1-RC1很顺利,2.1-RC2似乎也运行良好,直到现在。我已经创建了自己的Model便捷类来处理Mongo模型。可能有什么东西导致这种行为?
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "_class")
public class Model {
@JsonProperty("_id")
public ObjectId id;
public MongoCollection models() {
String collectionName = this.getClass().getName() + "s";
return PlayJongo.getCollection(collectionName);
}
public void save() {
models().save(this);
}
public void update() {
this.remove();
this.save();
}
public void remove() {
models().remove(this.id);
}
public static class Finder<T> {
private final Class<T> type;
public Finder(Class<T> type) {
this.type = type;
}
public MongoCollection models() {
String collectionName = type.getName() + "s";
return PlayJongo.getCollection(collectionName);
}
public List<T> all() {
List<T> list = new ArrayList<>();
Iterator<T> it = models().find().as(type).iterator();
while (it.hasNext()) {
list.add(it.next());
}
return list;
}
public T byId(ObjectId id) {
return models().findOne(id).as(type);
}
public T byUrl(String url) {
return models().findOne("{url: #}", url).as(type);
}
public List<T> byQuery(String query) {
List<T> list = new ArrayList<>();
Iterator<T> it = models().find(query).as(type).iterator();
while (it.hasNext()) {
list.add(it.next());
}
return list;
}
}
我的plugins.sbt:
// Comment to get more information during initialization
logLevel := Level.Warn
// The Typesafe repository
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
// Use the Play sbt plugin for Play projects
addSbtPlugin("play" % "sbt-plugin" % "2.1-RC2")
我的build.properties:
sbt.version=0.12.2-RC2
我的Build.scala:
import sbt._
import Keys._
import play.Project._
object ApplicationBuild extends Build {
val appName = "Test"
val appVersion = "0.1-SNAPSHOT"
val appDependencies = Seq(
javaCore,
"uk.co.panaxiom" %% "play-jongo" % "0.3"
)
val main = play.Project(appName, appVersion, appDependencies).settings(
lessEntryPoints <<= (sourceDirectory in Compile)(base => (
(base / "assets" / "css" / "bootstrap.less") +++
(base / "assets" / "css" / "responsive.less")
)
)
)
}
唯一可能相关的application.conf行:
playjongo.uri="mongodb://127.0.0.1:27017/test"
playjongo.gridfs.enabled=false
ehcacheplugin=enabled
堆栈跟踪:
! @6d48738mo - Internal server error, for (GET) [/] ->
play.api.Application$$anon$1: Execution exception[[RuntimeException: java.lang.NoSuchFieldError: Home]]
at play.api.Application$class.handleError(Application.scala:289) ~[play_2.10.jar:2.1-RC2]
at play.api.DefaultApplication.handleError(Application.scala:383) [play_2.10.jar:2.1-RC2]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anon$2$$anonfun$handle$1.apply(PlayDefaultUpstreamHandler.scala:114) [play_2.10.jar:2.1-RC2]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anon$2$$anonfun$handle$1.apply(PlayDefaultUpstreamHandler.scala:110) [play_2.10.jar:2.1-RC2]
at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) [play_2.10.jar:2.1-RC2]
at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) [play_2.10.jar:2.1-RC2]
java.lang.RuntimeException: java.lang.NoSuchFieldError: Home
at play.libs.F$Promise$6.apply(F.java:377) ~[play_2.10.jar:2.1-RC2]
at scala.concurrent.Future$$anonfun$map$1.liftedTree2$1(Future.scala:253) ~[scala-library.jar:na]
at scala.concurrent.Future$$anonfun$map$1.apply(Future.scala:249) ~[scala-library.jar:na]
at scala.concurrent.Future$$anonfun$map$1.apply(Future.scala:249) ~[scala-library.jar:na]
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:29) ~[scala-library.jar:na]
at akka.dispatch.BatchingExecutor$Batch$$anonfun$run$1.liftedTree1$1(BatchingExecutor.scala:67) ~[akka-actor_2.10.jar:na]
Caused by: java.lang.NoSuchFieldError: Home
at views.html.documents.homepage$.apply(homepage.template.scala:45) ~[na:na]
at views.html.documents.homepage$.render(homepage.template.scala:51) ~[na:na]
at views.html.documents.homepage.render(homepage.template.scala) ~[na:na]
at controllers.Home.read(Home.java:24) ~[na:na]
at controllers.Application.index(Application.java:24) ~[na:na]
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$17$$anonfun$apply$17.apply(routes_routing.scala:251) ~[na:na]
如果有人能告诉我这些错误可能来自哪里或指出我正确的方向,我将非常感激!如果您需要更多信息,请告诉我们!
答案 0 :(得分:2)
好吧,我在Play 2.1.0上遇到了完全相同的错误,花了两个晚上似乎我终于找到了问题所在!
我正在使用的模块有一个名为controllers.Application的控制器,其路由填充在模块的conf / routes文件中,我的项目中也有完全相同的控制器(也有路由)。它似乎是我的问题的根源。
那么请查看您的模块(或子项目?)是否存在冲突的控制器或路由,它应该可以解决您的问题。
答案 1 :(得分:0)
您应该在路线中附加参数类型。所以它应该是:
GET /homepage/:url/edit controllers.Home.edit(url: String)
Play文档中有几个例子没有这样做。但我认为声明explisit类型是最佳实践。
答案 2 :(得分:0)