我正在尝试将JSON字符串转换为我的模型类。我在play框架中使用morphia。
以下是相关的代码行:
public static void add(JsonObject body) {
System.out.println(body); // -> {"title":"test2","url":"http://www.gmx.de","description":"eine beschreibung für das lesezeichen","tags":["tag1","tag2","tag3","tag5","tag0209135913598"],"is_private":true}
Gson gson = new Gson();
Bookmark bookmark = gson.fromJson(body, Bookmark.class);
System.out.println(bookmark.getClass().getName()); // -> models.Bookmark
bookmark.save();
但我认为我做错了,因为这段代码抛出了以下异常:
Execution exception (In /app/controllers/Bookmarks.java around line 40)
IllegalArgumentException occured : can't serialize class java.lang.Object
play.exceptions.JavaExecutionException: can't serialize class java.lang.Object
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:231)
at Invocation.HTTP Request(Play!)
Caused by: java.lang.IllegalArgumentException: can't serialize class java.lang.Object
at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:234)
at org.bson.BSONEncoder.putIterable(BSONEncoder.java:259)
at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:198)
at org.bson.BSONEncoder.putObject(BSONEncoder.java:140)
at org.bson.BSONEncoder.putObject(BSONEncoder.java:86)
at com.mongodb.OutMessage.putObject(OutMessage.java:190)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:253)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:217)
at com.mongodb.DBCollection.insert(DBCollection.java:71)
at com.mongodb.DBCollection.save(DBCollection.java:633)
at com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:764)
at com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:826)
at com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:820)
at play.modules.morphia.Model.save2(Model.java:705)
at play.modules.morphia.Model.save(Model.java:692)
at controllers.Bookmarks.add(Bookmarks.java:40)
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)
... 1 more
似乎以下行抛出此异常:
bookmark.save();
以下是模型的代码:
@AutoTimestamp
@Entity
public class Bookmark extends Model {
public String title;
public String url;
public String description;
@Indexed
public List tags;
public boolean is_private;
public Bookmark() {}
public Bookmark(String title,
String url,
String description,
List tags,
boolean is_private)
{
this.title = title;
this.url = url;
this.description = description;
this.tags = tags;
this.is_private = is_private;
}
}
我现在的问题是如何解决这个问题,或者我如何根据json输入创建模型实例?
答案 0 :(得分:0)
由于fromJson()只接受String
或Reader
个对象而导致JsonObject
传递错误,因此会抛出错误。
但是因为玩!使用POJO binding它可能更好地做类似的事情:
public static void add(Bookmark newBookmark) {
// Validation etc ..
bookmark.save();
}
如果你真的想与Gson合作,那么这样的事情可能有用(基于the docs):
public static void add(JsonObject body) {
Gson gson = new Gson();
String json = gson.toJson(body, Bookmark.class);
Bookmark bookmark = gson.fromJson(json, Bookmark.class);
bookmark.save();
}