在Play Framework URL中传递MongoDB ObjectId(Passing MongoDB ObjectId in Play Framework URL)

嗨,我正在使用Java学习Play Framework 2并遇到问题。 我使用MongoDB,并有一个简单的类User和ObjectId作为唯一ID。

public class User { @JsonProperty public ObjectId id; ..

在我看来,我想添加一个按钮来删除当前用户,如下所示:

@form(routes.Application.deleteUser(user.id)) { <input type="submit" value="Delete"> }

在我的路线文件中:

POST /users/:id/delete controllers.Application.deleteUser(id: org.bson.types.ObjectId)

但是现在我收到了一个错误:

“找不到类型为org.bson.types.ObjectId的URL路径绑定器。尝试为此类型实现隐式PathBindable”

我尝试了很多东西,例如我试图只将ObjectId值作为String传递,但没有任何东西对我有用。 有人可以帮我这个吗?

Hi I'm learning Play Framework 2 with Java and have a problem. I use MongoDB, and have a simple class User with ObjectId as unique id.

public class User { @JsonProperty public ObjectId id; ..

in my view I want to add a button to delete current user, something like this :

@form(routes.Application.deleteUser(user.id)) { <input type="submit" value="Delete"> }

and in my routes file :

POST /users/:id/delete controllers.Application.deleteUser(id: org.bson.types.ObjectId)

But now I got an error :

"No URL path binder found for type org.bson.types.ObjectId. Try to implement an implicit PathBindable for this type"

I tried a lot of things, for example I tried to pass only the ObjectId value as a String, but nothing worked for me. Can anyone please help me with this ?

最满意答案

您可以使用具有必要绑定器的play-salat ,只需将其作为依赖项添加到您的project/Build.scala并将其导入您的路由和模板:

import sbt._ import Keys._ import PlayProject._ object ApplicationBuild extends Build { val appDependencies = Seq( "se.radley" %% "play-plugins-salat" % "1.2-SNAPSHOT" ) val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).settings( resolvers += "OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/", routesImport += "se.radley.plugin.salat.Binders._", templatesImport += "org.bson.types.ObjectId" ) }

另请参阅此示例应用程序 。

You could use play-salat which have neccessary binders, just add it as a dependency to your project/Build.scala and import it to your routes and templates:

import sbt._ import Keys._ import PlayProject._ object ApplicationBuild extends Build { val appDependencies = Seq( "se.radley" %% "play-plugins-salat" % "1.2-SNAPSHOT" ) val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).settings( resolvers += "OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/", routesImport += "se.radley.plugin.salat.Binders._", templatesImport += "org.bson.types.ObjectId" ) }

Also take a look to this example application.

更多推荐