欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

利用JsonConfig消除JSON转换出现双向关联循环问题

程序员文章站 2022-07-12 15:53:01
...
今天在利用JSONArray.fromObject把对象转成JSON的时候,总是报如下错误:
2011-10-10 13:52:25 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet default threw exception
java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407)
at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:801)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:519)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)


百思不得其解,把对象一一遍历打印出后,发现原来是多个多想之间存在双向关联,再转成JSON的过程中,出现死循环。如下


@OneToMany(mappedBy="customer",cascade=CascadeType.PERSIST,fetch=FetchType.LAZY)
private List<OrderManage> orderManageList; //关于订单管理 by 2011-09-19]

@OneToMany(mappedBy="consumersCust",fetch=FetchType.EAGER)
private Set<Consumers> consumers = new HashSet<Consumers>();//商机



利用JsonConfig解决剔除掉关联字段,解决了此问题。


JsonConfig config = new JsonConfig();
//排除关联对象属性,排除日期两个属性
config.setExcludes(new String[]{"birthday","idcard"});
config.setJsonPropertyFilter(new PropertyFilter(){
public boolean apply(Object source, String name, Object value) {
if(name.equals("linkMans")||name.equals("consumers")||name.equals("orderManageList")) {
return true;
} else {
return false;
}
}
});
//JsonConfig 还提供了JSON转换功能,也能方便
config.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessorDate());