您好我正在使用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传递,但没有任何东西对我有效。有人可以帮我这个吗?
答案 0 :(得分:4)
您可以使用具有必要绑定器的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"
)
}
另请查看此example application。
答案 1 :(得分:0)
你可能会在寻找Morphia,MongoDB ORM for Java,毕竟? 对于Morphia,Slideshare中的良好启动可能是this tutorial。
顺便说一句,我发现_id
用户名字符串更好地反对Mongo的ObjectId。
小例子:
//Routes
GET /add/:username controllers.Application.createTestPerson(username)
GET /delete/:username controllers.Application.delete(username)
//Controller
public class Application extends Controller {
...
public static Result createTestPerson(String username){
//DB connection and Morphia Datastore
DBConn conn = new DBConn("test");
Datastore ds = conn.getDatastore();
//Person document for saving
Person person = new Person(username);
person.setName("John", "Doe");
//save person to Mongo
ds.save(person);
return ok("user \""+username+"\" saved");
}
public static Result delete(String username){
//DB connection and Morphia Datastore
DBConn conn = new DBConn("test");
Datastore ds = conn.getDatastore();
ds.delete(Person.class,username);
return ok("user \""+username+"\" deleted");
}
}
//models Person.java
import com.google.code.morphia.annotations.*;
import org.bson.types.ObjectId;
@Entity("persons")
public class Person {
@Id
String userName;
Name name;
public Person(String u){ userName = u; }
public void setName(String first, String last){
name = new Name(first, last);
}
}
@Embedded
class Name {
String first, last;
public Name(){ }
public Name(String first, String last) {
this.first = first;
this.last = last;
}
}
//models DBConn.java
import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.mongodb.Mongo;
import java.net.UnknownHostException;
public class DBConn implements AutoCloseable{
Morphia morphia;
Mongo mongo;
Datastore ds;
public DBConn(){
new DBConn("test");
}
public DBConn(String collection){
morphia = new Morphia();
try {
mongo = new Mongo();
} catch (UnknownHostException ex) {
System.out.println("[Error] MongoDB Error");
}
ds = morphia.createDatastore(mongo, collection);
System.out.println("DB conn success ["+ ds.getDB().getName() + "]");
}
public Datastore getDatastore(){
return ds;
}
public void close() throws Exception {
mongo.close();
}
}
所以用
localhost:9000/delete/what-ever-here
localhost:9000/createTestPerson/what-ever-here
您应该能够管理Mongo Collection并在Mongo控制台中查看结果:
> db.persons.find()
{ "_id" : "johndoe", "className" : "models.Person", "name" : { "first" : "John",
"last" : "Doe" } }
>
答案 2 :(得分:0)
对于使用Java的Play 2.3.x,在build.sbt
上import play.PlayImport.PlayKeys._
name := "test"
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayJava)
scalaVersion := "2.11.6"
libraryDependencies ++= Seq(
javaJdbc,
cache,
javaWs,
filters,
"org.mongodb" % "mongo-java-driver" % "3.0.1",
"se.radley" %% "play-plugins-salat" % "1.5.0"
)
val main = Project("test", file(".")).enablePlugins(play.PlayJava).settings(
routesImport += "se.radley.plugin.salat.Binders._"
)
在您的路线上,您可以通过以下方式使用
controllers.Application.index(id: ObjectId)