Spring MVC Json解析限制的最佳解决方法 [英] Best workaround for Spring MVC Json parsing limitations

查看:141
本文介绍了Spring MVC Json解析限制的最佳解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Spring,Hibernate的项目,并且有一个返回JSON的控制器。当然,我的模型包含使用JPA注释定义hibernate关系的列表等等,例如,我有Users,它们包含一组他们拥有的挑战,同样Challenge包含拥有它的用户。

I have a project which uses Spring, Hibernate, and has controllers which return JSONs. Naturally, my models contain lists and such to use JPA annotations to define the hibernate relationships, so, for example, I have Users, which contain a set of Challenges they own, and likewise Challenge contains a User who owns it.

不幸的是,我的JSON中嵌入的集合似乎遇到了很多问题。

Unfortunately I seem to be having a lot of issues with collections embedded in my JSONs.

例如,使用该设置(一个用户拥有挑战,挑战有一个拥有者)我可以回答挑战就好了。我可以回复用户就好了。但是,当我尝试返回一系列挑战时,一切都会爆炸!我从Jmeter测试中收到以下错误:

For example, with that set up (a User owns challenges and a challenge has a owner) I can return a Challenge just fine. I can return a User just fine. But when I try to return a list of challenges, everything blows up! I receive the following error from my Jmeter test:

错误500服务器错误

我相信这意味着杰克逊json解析器设置json有一个问题。我相信这一点,因为如果我使用@JsonIgnoreProperties({challengeOwned}),那么我可以很好地返回挑战列表,因为每个单独的挑战对象不再嵌入其中的列表。

I believe this means that the Jackson json parser had an issue setting the json. I believe this, because if I use @JsonIgnoreProperties({"challengesOwned"}) then I can return the list of challenges just fine, since each individual challenge object no longer has a list embedded inside it.

这对我来说似乎很奇怪。杰克逊真的不能在JSON中映射简单的嵌入式列表吗?我也遇到了一个很大的问题,因为我有一个使用User作为其键的Map ...而且似乎甚至无法将JSON map的键定义为嵌入对象!

This seems very strange to me. Can Jackson really not map simple embedded lists within JSONs? I've also got a huge problem because I have a Map which uses a User as its key ... and it seems it's not even possible to define a JSON map's key as an embedded object at all!

有没有人对我的问题有任何建议?我是否必须手动定义一些Json映射?有一个我不知道的简单解决方案吗?

Does anyone have a suggestion for my issue? Do I have to manually define some Json mappings? Is there a simple solution I just don't know about?

编辑:

虽然j0ntech说的是什么似乎是真实的,事实证明这不是整个故事。似乎当Spring使用Jackson将我的一个hibernate实体序列化为它的JSON版本时,hibernate试图延迟加载该实体的一个属性,但由于该实体在该点处处于其事务之外(在控制器中) ),它引起了一个例外,它被吞没了。

While what j0ntech says does seem to have been true, it turns out that was not the whole story. It seems that when Spring used Jackson to serialize one of my hibernate entities into it's JSON version, hibernate was trying to lazy load one of that entity's properties, but since the entity was outside of its transaction at that point (being "in" the controller), it caused an exception, which just got swallowed up.

所以实际上有两个问题。我想通过尝试手动使用Jackson来序列化我实际返回之前返回的对象来解决这个问题。这样我实际上得到了另一个问题的堆栈跟踪。

So there were actually TWO issues. I figured this out by trying to manually use Jackson to serialize the object I was returning before actually returning it. That way I actually got the stack trace for the other issue.

推荐答案

你可能有一个递归循环(根据DwB的评论) :用户包含挑战列表,每个挑战包含一个用户,其中包含挑战列表,依此类推。解析器(或整个服务器)不喜欢这样。您应该使用注释 JsonManagedReference JsonBackReference

You probably have a recursive loop (as per DwB's comment): User contains a list of Challenges, which each contain a User, which contains a list of Challenges and so on and so forth. The parser (or your server at large) doesn't like that. You should use the annotations JsonManagedReference and JsonBackReference.

您可以在此处上阅读有关如何使用这些注释的信息。 href =http://www.javacodegeeks.com/2012/09/json-jackson-to-rescue.html =nofollow>这里。我已经在我自己的一些项目中使用它们,如果正确实现它们的效果非常好。

You can read about how to use these annotations here and here. I've used them in some of my own projects and they work very well if correctly implemented.

这篇关于Spring MVC Json解析限制的最佳解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆