Passing a MongoDB ObjectId to a Playback Platform URL

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

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

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

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

and in the routes file:

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

But now I got the error message:

"There is no middleware path URL for type org.bson.types.ObjectId. Try to implement an implicit PathBindable for this type

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

+4
source share
3 answers

You can use play-salat , which have the necessary binders, just add it as a dependency to your project/Build.scala and import it into 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 consider an example.

+4
source

Perhaps you are looking for Morphia , ORM for MongoDB for Java, afterall? For Morphia, this tutorial at Slideshare might be a good start.

Btw, I find the _id String username is better than contra Mongo ObjectId.

A small example:

 //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(); } } 

So,

 localhost:9000/delete/what-ever-here localhost:9000/createTestPerson/what-ever-here 

You can manage the Mongo collection and view the results in the Mongo console:

 > db.persons.find() { "_id" : "johndoe", "className" : "models.Person", "name" : { "first" : "John", "last" : "Doe" } } > 
0
source

To play 2.3.x with Java, on your 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._" ) 

and on your routes you can use it below.

 controllers.Application.index(id: ObjectId) 
0
source

All Articles