WebService调用一对多关联关系时出现 死循环:A cycle is detected in...
来源:互联网 发布:js 一分钟倒计时代码 编辑:程序博客网 时间:2024/05/17 08:11
通过WebService调用一对多关联关系时引起的问题:A cycle is detected in the object graph
具体异常信息:
org.apache.cxf.interceptor.Fault: Marshalling Error: A cycle is detected in the object graph. This will cause infinitely deep XML: cn.jssms.platform.model.system.AppUser@75fce7 ->cn.jssms.platform.model.system.AppRole@195266b -> cn.jssms.platform.model.system.AppUser@75fce7
AppUser-->AppRole-->AppUser
在网上查阅资料后发现两个VO类中存在关联关系,可能是1:n 或n:1关系,应该跟Hibernate反射有关系。
循环引用:
Parent 和 Child是1:n的关系, Parent含有一个child的列表children,child对于parent有一个引用,那这两个对象之间就存在循环引用的关系.
我们不能 直接将带有环的对象暴露给webservice,因为这会导致最终生成xml的时候会陷入死循环最后栈溢出,所以cxf检测出对象存在cycle会抛出一 个异常阻止进一步发布webservice.(以前的XFire可没这么聪明,它没有检测机制,直接就去序列化xml结果会导致 OutOfMemory).
如何解决呢?就是要破掉这个环,去某一端,如何在不破坏原有设计的情况下做到这一点,就是要使用@XmlTransient
这个annotation会标明这个字段不要解析成xml,所有你不想解析进webservice的都可以通过这个标签来标记
注:cxf默认采用JAXB做databindings,如果要用aegis,相应的就要用@IgnoreProperty这个元注释
像这种情况,我们一般要打破子对父的引用,就是要打破Child对于Parent的引用.注意要在parent的get方法上面加而不是在parent的声明处.
这样从生成的wsdl里面我们就看不到child里有对于parent的引用
虽然client能够拿到children列表了,但通过child得不到parent的信息,因为在client现在是单向的,那我也想访问parent怎么办?
这里有一个解决办法,在Parent下面加入如下代码:
public void afterUnmarshal(Unmarshaller u, Object parent) {
this.parent = (Parent) parent;
}
怎么做到的?背后的道理是这样的:
循环的双向关系,一端到另一端的关系确定了以后,反过来另一端也就确定了.
cxf在解析wsdl映射成对象的过程中(也就是unmarshal),处理Parent并处理它所包含的child,结果发现parent引 用的child有afterUnmarshal方法,然后把自己的引用通过该方法传递给child,这样child也具有了对于 parent的引用,这一切都是在客户端完成的.真的是很聪明的做法.
这些功能必须要cxf来做客户端才能实现,但我们可以利用这种技术来在其他客户端实现这个功能.比如flex,.net, php 等等.
one more thing
上面的例子首先访问的是parent,cxf可以拿到两端的信息,但如果先访问child就拿不到parent了.
那么如果把@XmlTransient加到Parent, 同样道理, child可以得到parent的信息,但是这个parent的getChildren里恐怕就只有那一个child了.
所以还是看具体设计,如果parent需要经常访问child,第一种最好,如果child要经常访问parent,显然是第二种
- WebService调用一对多关联关系时出现 死循环:A cycle is detected in...
- WebService调用一对多关联关系时引起问题:A cycle is detected in the object graph
- JSON解析关联类型发生死循环 There is a cycle in the hierarchy!
- 关于一对多的关联关系 spring json出现死循环的问题
- JSONException: There is a cycle in the hierarchy!解决Json死循环问题
- JSON 转换异常 死循环 There is a cycle in the hierarchy
- 解决json,There is a cycle in the hierarchy,只循环到某一个关联层
- 出现“There is a cycle in the hierarchy”异常
- 导入项目出现a cycle was detected in the build path of project问题解决
- Json解析时出现net.sf.json.JSONException: There is a cycle in the hierarchy!42
- 黑马bos项目运行json时出现There is a cycle in the hierarchy!的解决办法
- jQuery调用JSON时,net.sf.json.JSONException: There is a cycle in the hierarchy
- jQuery调用JSON时,net.sf.json.JSONException: There is a cycle in the hierarchy
- jQuery调用JSON时,net.sf.json.JSONException: There is a cycle in the hierarchy
- jQuery调用JSON时,net.sf.json.JSONException: There is a cycle in the hierarchy!
- Hibernate 一对多关联关系
- 映射一对多关联关系
- 映射一对多关联关系
- Flex之自定义事件
- Java自带webservice
- webservice注释
- JAVA6学习webservice
- datagrid和combobox简单应用
- WebService调用一对多关联关系时出现 死循环:A cycle is detected in...
- Flex之DataGrid和Tree控件的数据源XML格式
- Flex应用一览表
- Flex控件之repeater和radioButton应用
- cas server 配置一些警告问题
- Codeforces Round #230 (Div. 2) C: Blocked Points 水泄不通的点墙
- 静态链表实现的两种方法
- HTML5标签改变
- Iframe的应用以及父窗口和子窗口的相互访问