gpt4 book ai didi

java - Playframework,将 JSON 转换为模型(使用 Gson),保存模型失败

转载 作者:行者123 更新时间:2023-12-01 05:26:17 24 4
gpt4 key购买 nike

我正在尝试将 JSON 字符串转换为我的模型类。我在游戏框架内使用吗啡。

以下是相关代码行:

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 输入创建模型实例?

最佳答案

它抛出一个错误,因为 fromJson()仅接受 StringReader 对象,您传入的是 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();
}

关于java - Playframework,将 JSON 转换为模型(使用 Gson),保存模型失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9636727/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com