玩!使用 JQuery 渲染框架示例列表
需要一些帮助,我有一个服务器端控制器功能:
public static void getNews(){
List<Thread> threads = Thread.findAll();
renderJSON(threads);
}
和前端请求:
<script type="text/javascript">
$.getJSON('@{Application.getNews()}', function(threads) {
var items = [];
$.each(threads, function(title, text) {
items.push("<div>" + text + "</div>");
});
});
</script>
但它不起作用。控制台异常列表:
Internal Server Error (500) for request GET /application/getnews
Execution exception (In /app/controllers/Application.java around line 43)
IllegalStateException occured : circular reference error Offending field: thre
ads Offending object: preserveType: true, type: class models.Thread, obj: Thr
ead[21]
play.exceptions.JavaExecutionException: circular reference error
Offending field: threads
Offending object: preserveType: true, type: class models.Thread, obj: Thread[2
1]
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:227)
at Invocation.HTTP Request(Play!)
Caused by: java.lang.IllegalStateException: circular reference error
Offending field: threads
Offending object: preserveType: true, type: class models.Thread, obj: Thread[2
1]
at com.google.gson.CircularReferenceException.createDetailedException(Ci
rcularReferenceException.java:43)
at com.google.gson.JsonSerializationVisitor.visitFieldUsingCustomHandler
(JsonSerializationVisitor.java:212)
at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.j
ava:158)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:131)
at com.google.gson.JsonSerializationVisitor.getJsonElementForChild(JsonS
erializationVisitor.java:147)
at com.google.gson.JsonSerializationVisitor.addAsChildOfObject(JsonSeria
lizationVisitor.java:125)
at com.google.gson.JsonSerializationVisitor.visitObjectField(JsonSeriali
zationVisitor.java:112)
at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.j
ava:164)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:131)
at com.google.gson.JsonSerializationContextDefault.serialize(JsonSeriali
zationContextDefault.java:56)
at com.google.gson.JsonSerializationContextDefault.serialize(JsonSeriali
zationContextDefault.java:49)
at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(D
efaultTypeAdapters.java:528)
at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(D
efaultTypeAdapters.java:510)
at com.google.gson.JsonSerializationVisitor.findAndInvokeCustomSerialize
r(JsonSerializationVisitor.java:184)
at com.google.gson.JsonSerializationVisitor.visitUsingCustomHandler(Json
SerializationVisitor.java:160)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:106)
at com.google.gson.JsonSerializationContextDefault.serialize(JsonSeriali
zationContextDefault.java:56)
at com.google.gson.Gson.toJsonTree(Gson.java:233)
at com.google.gson.Gson.toJson(Gson.java:273)
at com.google.gson.Gson.toJson(Gson.java:253)
at play.mvc.results.RenderJson.<init>(RenderJson.java:20)
at play.mvc.Controller.renderJSON(Controller.java:325)
at controllers.Application.getNews(Application.java:43)
at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:540)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:498)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:474)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:469)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:157)
... 1 more
Caused by: com.google.gson.CircularReferenceException: circular reference error
at com.google.gson.JsonSerializationVisitor.start(JsonSerializationVisit
or.java:56)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:114)
at com.google.gson.JsonSerializationContextDefault.serialize(JsonSeriali
zationContextDefault.java:56)
at com.google.gson.JsonSerializationContextDefault.serialize(JsonSeriali
zationContextDefault.java:49)
at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(D
efaultTypeAdapters.java:528)
at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(D
efaultTypeAdapters.java:510)
at com.google.gson.JsonSerializationVisitor.findAndInvokeCustomSerialize
r(JsonSerializationVisitor.java:184)
at com.google.gson.JsonSerializationVisitor.visitFieldUsingCustomHandler
(JsonSerializationVisitor.java:203)
... 27 more
need some help, i have a server-side Contoller function:
public static void getNews(){
List<Thread> threads = Thread.findAll();
renderJSON(threads);
}
And front-end request:
<script type="text/javascript">
$.getJSON('@{Application.getNews()}', function(threads) {
var items = [];
$.each(threads, function(title, text) {
items.push("<div>" + text + "</div>");
});
});
</script>
But it doesn't work. Console exception listing:
Internal Server Error (500) for request GET /application/getnews
Execution exception (In /app/controllers/Application.java around line 43)
IllegalStateException occured : circular reference error Offending field: thre
ads Offending object: preserveType: true, type: class models.Thread, obj: Thr
ead[21]
play.exceptions.JavaExecutionException: circular reference error
Offending field: threads
Offending object: preserveType: true, type: class models.Thread, obj: Thread[2
1]
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:227)
at Invocation.HTTP Request(Play!)
Caused by: java.lang.IllegalStateException: circular reference error
Offending field: threads
Offending object: preserveType: true, type: class models.Thread, obj: Thread[2
1]
at com.google.gson.CircularReferenceException.createDetailedException(Ci
rcularReferenceException.java:43)
at com.google.gson.JsonSerializationVisitor.visitFieldUsingCustomHandler
(JsonSerializationVisitor.java:212)
at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.j
ava:158)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:131)
at com.google.gson.JsonSerializationVisitor.getJsonElementForChild(JsonS
erializationVisitor.java:147)
at com.google.gson.JsonSerializationVisitor.addAsChildOfObject(JsonSeria
lizationVisitor.java:125)
at com.google.gson.JsonSerializationVisitor.visitObjectField(JsonSeriali
zationVisitor.java:112)
at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.j
ava:164)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:131)
at com.google.gson.JsonSerializationContextDefault.serialize(JsonSeriali
zationContextDefault.java:56)
at com.google.gson.JsonSerializationContextDefault.serialize(JsonSeriali
zationContextDefault.java:49)
at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(D
efaultTypeAdapters.java:528)
at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(D
efaultTypeAdapters.java:510)
at com.google.gson.JsonSerializationVisitor.findAndInvokeCustomSerialize
r(JsonSerializationVisitor.java:184)
at com.google.gson.JsonSerializationVisitor.visitUsingCustomHandler(Json
SerializationVisitor.java:160)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:106)
at com.google.gson.JsonSerializationContextDefault.serialize(JsonSeriali
zationContextDefault.java:56)
at com.google.gson.Gson.toJsonTree(Gson.java:233)
at com.google.gson.Gson.toJson(Gson.java:273)
at com.google.gson.Gson.toJson(Gson.java:253)
at play.mvc.results.RenderJson.<init>(RenderJson.java:20)
at play.mvc.Controller.renderJSON(Controller.java:325)
at controllers.Application.getNews(Application.java:43)
at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:540)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:498)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:474)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:469)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:157)
... 1 more
Caused by: com.google.gson.CircularReferenceException: circular reference error
at com.google.gson.JsonSerializationVisitor.start(JsonSerializationVisit
or.java:56)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:114)
at com.google.gson.JsonSerializationContextDefault.serialize(JsonSeriali
zationContextDefault.java:56)
at com.google.gson.JsonSerializationContextDefault.serialize(JsonSeriali
zationContextDefault.java:49)
at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(D
efaultTypeAdapters.java:528)
at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize(D
efaultTypeAdapters.java:510)
at com.google.gson.JsonSerializationVisitor.findAndInvokeCustomSerialize
r(JsonSerializationVisitor.java:184)
at com.google.gson.JsonSerializationVisitor.visitFieldUsingCustomHandler
(JsonSerializationVisitor.java:203)
... 27 more
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 Play 中默认 JSON 引擎 GSON 的问题。切换到 Flex.JSON
解决您的具体问题 - http://www.lunatech-research.fr/archives/2011/04/20/play-framework-better-json-serialization-flexjson
This is an issue with GSON the default JSON engine in Play. Switch to Flex.JSON
Solution to your exact problem - http://www.lunatech-research.fr/archives/2011/04/20/play-framework-better-json-serialization-flexjson
试试这个。
在 URL 中放置一个简单的路由
JS URL ref
'route/'
在路由配置文件中添加此内容,其中 method 是控制器函数的名称。
GETroute/controllers.Application.method()
这应该有帮助。
Try this.
In the URL place a simple route
JS URL ref
'route/'
In the routes conf file add this where method is the name of the controller function.
GET route/ controllers.Application.method()
That should help.