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开发者_Go百科.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
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
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.
精彩评论