4

嗨,我正在使用 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 值作为字符串传递,但对我没有任何效果。谁能帮我解决这个问题?

4

3 回答 3

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

另请查看此示例应用程序

于 2012-12-29T07:38:17.050 回答
0

毕竟,您可能正在寻找Morphia、 MongoDB ORM for Java 吗?对于Morphia来说,Slideshare 中的本教程可能是一个很好的起点。

顺便说一句,我发现_id用户名 String 比 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" } }
>
于 2012-12-30T04:26:09.693 回答
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)
于 2015-07-08T13:51:23.187 回答