Best Workaround for Spring MVC Json Parsing Limitations

I have a project that uses Spring, Hibernate and has controllers that return JSON. Naturally, my models contain lists, etc., To use JPA annotations to define hibernate relationships, so for example, I have users that contain a set of problems that they have, and also the Challenge contains a user who owns it.

Unfortunately, I seem to have a lot of problems with collections built into my JSON.

For example, with this setting (the user owns the tasks, and the call has the owner), I can return the call just fine. I can return the user just fine. But when I try to return the list of problems, everything explodes! I get the following error from the Jmeter test:

Error 500 Server Server

I believe this means that the Jackson json parser had a problem setting json. I believe in this because if I use @JsonIgnoreProperties ({"tasksOwned"}), then I can return the list of tasks just fine, since each individual request object no longer has a list built into it.

It seems very strange to me. Can Jackson really not display simple inline lists in JSON? I also have a huge problem, because I have a map in which the user uses his key ... and it seems even impossible to define the JSON map key as an inline object in general!

Does anyone have a suggestion on my problem? Do I need to manually define some Json mappings? Is there a simple solution that I just don’t know about?

EDIT:

Although what j0ntech says seemed to be true, it turns out that this was not the whole story. It seems that when Spring used Jackson to serialize one of my sleeping entities in the JSON version, hibernate tried to be lazy to load one of these properties of the object, but since the object was outside its transaction at this point (being "in", the controller), it caused exception that has just been swallowed.

So there were actually two problems. I thought about this while trying to manually use Jackson to serialize the object I was returning before returning it. So I got a stack trace for another problem.

+4
source share
3 answers

You probably have a recursive loop (according to DwB's comment): The user contains a list of problems, each of which contains a user that contains a list of calls, etc. etc. The parser (or your server as a whole) does not like it. You should use the JsonManagedReference and JsonBackReference annotations .

You can read about how to use these annotations here and here . I used them in some of my projects, and they work great if they are implemented correctly.

+2
source

you can try flexjson (used by Spring Roo) or gson (developed by google)

0
source
0
source

All Articles