在使用Morphia的Play Framework中,如何更新对象?

时间:2012-05-15 14:45:11

标签: java playframework morphia

播放1.2.4 Morphia 1.2.6a

我试图在很多天里用几种不同的方式更新记录。我已经阅读了几本书,几乎评论了Play上的每一条评论,我仍然无法弄清楚这一点。???

MODEL

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

路线

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

发送

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

调用Update1,Update2和Update3,所有报告异常并退出。

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)

更新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

帮助?

1 个答案:

答案 0 :(得分:3)

我认为这更多是关于JSON绑定而不是morphia问题。 AFAIK,播放1.x不支持JSON自动绑定。所以你可能需要的是:

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

在您的客户端,您必须发送名为“myData”的ajax消息:

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