0

玩 1.2.4 Morphia 1.2.6a

在太多天里,我试图以几种不同的方式更新记录。我已经阅读了几本书,几乎查看了关于 Play 的所有评论,但我仍然无法弄清楚这一点。???

型号

@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')

控制器

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,均报异常退出。

呼叫更新1:

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)

呼叫更新2:

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

帮助?

4

1 回答 1

3

我认为这更多是关于 JSON 绑定而不是 morphia 问题。AFAIK,play 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){...});
于 2012-05-15T20:15:55.980 回答