4

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 ?

biesior
  • 55,576
  • 10
  • 125
  • 182
Dawid
  • 644
  • 1
  • 14
  • 30
  • There are binders in Scala, take a look, may be you could rewrite it in Java https://github.com/leon/play-salat/blob/master/src/main/scala/se/radley/plugin/salat/Binders.scala – lambdas Dec 29 '12 at 07:31

3 Answers3

4

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.

lambdas
  • 3,990
  • 2
  • 29
  • 54
  • That would be great, but I need something in Java – Dawid Dec 29 '12 at 14:19
  • 1
    I know, but still think it would work for you. It is ok to use Scala libraries in Java application. – lambdas Dec 29 '12 at 15:43
  • I got another problem. There is an error : [NullPointerException: null] on line : Call("POST", "/users/" + implicitly[PathBindable[ObjectId]].unbind("id", id) + "/delete") – Dawid Dec 30 '12 at 12:36
  • Are you sure you passing id in this line `@form(routes.Application.deleteUser(user.id)) {`? Could you check by displaying it? It looks like your `user` is null. – lambdas Dec 30 '12 at 15:07
  • Thats a strange thing. For example in mongo console : > db.users.find() { "_id" : ObjectId("50d76439ba472c9cee2c5435"), "name" : "Franek" } { "_id" : ObjectId("50e0a25c4fbea3f0450d3690"), "name" : "Ziomek" } But when I want to display it in application : @for(user <- users) {
  • @(user.id)
  • @(user.name)
  • } I see only names, no id! – Dawid Dec 30 '12 at 20:30
  • Ok I found what was wrong there. I just didnt add '("_id")' to the id field in User class. Thanks for your help! – Dawid Dec 30 '12 at 21:04