Search code examples
javaplayframeworkmorphia

In PlayFramework with Morphia, how do I update an object?


Play 1.2.4 Morphia 1.2.6a

I have tried to update a record a few different ways over far too many days. I have read a couple books, reviewed nearly every comment on Play and I still cannot figure this out.???

MODEL:

@Entity
public class MyModel extends Model{
   public String fullname;
   public String email;
}

ROUTES:

PUT /test1/? Application.Update1(format:'json')
PUT /test2/? Application.Update2(format:'json')
PUT /test3/? Application.Update2(format:'json')

CONTROLLER:

public static void Update1(MyModel mydata){
}

public static void Update2(JsonObject json){
}

public static void Update3(@Valid MyModel mydata){
}

SEND:

PUT
Content-Type: application/json
Accept: application/json
{ "id": "4fa932fa036494e78debbc14",
  "fullname" : "test"
}

Calling Update1, Update2, and Update3, all report exceptions and quit.

CALL UPDATE1:

Oops: RuntimeException
An unexpected error occured caused by exception RuntimeException: play.exceptions.UnexpectedException: Unexpected Error

play.exceptions.UnexpectedException: Unexpected Error
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:258)
    at Invocation.HTTP Request(Play!)
Caused by: java.lang.RuntimeException: play.exceptions.UnexpectedException: Unexpected Error
    at play.modules.morphia.Model.create(Model.java:73)

CALL UPDATE2:

NullPointerException occured : null

play.exceptions.JavaExecutionException
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:231)
    at Invocation.HTTP Request(Play!)
Caused by: java.lang.NullPointerException
    at controllers.ApplicationController.Update2(ApplicationController.java:49)
    at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:548)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:502)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:478)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:473)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:161)

UPDATE 3:

Oops: RuntimeException
An unexpected error occured caused by exception RuntimeException: play.exceptions.UnexpectedException: Unexpected Error

play.exceptions.UnexpectedException: Unexpected Error
    at play.data.validation.ValidationPlugin.beforeActionInvocation(ValidationPlugin.java:59)
    at play.plugins.PluginCollection.beforeActionInvocation(PluginCollection.java:594)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:134)
    at Invocation.HTTP Request(Play!)
Caused by: java.lang.RuntimeException: play.exceptions.UnexpectedException: Unexpected Error
    at play.modules.morphia.Model.create(Model.java:73)
    at play.modules.morphia.MorphiaPlugin.bind(MorphiaPlugin.java:601)
    at play.PlayPlugin.bind(PlayPlugin.java:68)
    at play.plugins.PluginCollection.bind(PluginCollec

Help?


Solution

  • I think this is more about JSON binding than a morphia problem. AFAIK, play 1.x does not support JSON automatic bind. So probably what you need is:

    public static void Update(String myData){
       MyModel model = new Gson().fromJSON(myData, MyModel.class);
       ...
    }
    

    In your client side, you must send a ajax message with the name "myData":

    $.post("the/url", {myData: {...}}, function(response){...});