JsonConfig的jsonConfig.setExcludes的用法

前端之家收集整理的这篇文章主要介绍了JsonConfig的jsonConfig.setExcludes的用法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

JsonConfig的jsonConfig.setExcludes的用法

之前写一个小东西,当用JSONArray.fromObject()方法转一个对象集合时,因为对象中存在一对多的关系,一直抛出异常

严重: Error executing FreeMarker template
FreeMarker template error:
Java method “org.apache.commons.lang.exception.NestableRuntimeException.getMessage(int)” threw an exception when invoked on net.sf.json.JSONException object “net.sf.json.JSONException: org.hibernate.LazyInitializationException: Failed to lazily initialize a collection of role: cn.zzuli.app.domain.User.positions,no session or session was closed”; see cause exception in the Java stack trace.


FTL stack trace (“~” means nesting-related):

- Failed at: ${msg[0]?html} [in template “org/apache/struts2/dispatcher/error.ftl” at line 68,column 29]

Java stack trace (for programmers):

freemarker.core._TemplateModelException: [… Exception message was already printed; see it above …]
at freemarker.ext.beans._MethodUtil.newInvocationTemplateModelException(_MethodUtil.java:311)
at freemarker.ext.beans._MethodUtil.newInvocationTemplateModelException(_MethodUtil.java:275)
at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:80)
at freemarker.ext.beans.SimpleMethodModel.get(SimpleMethodModel.java:86)
at freemarker.core.DynamicKeyName.dealWithNumericalKey(DynamicKeyName.java:104)
at freemarker.core.DynamicKeyName._eval(DynamicKeyName.java:69)
at freemarker.core.Expression.eval(Expression.java:78)
at freemarker.core.Expression.evalAndCoerceToString(Expression.java:82)
at freemarker.core.BuiltInForString._eval(BuiltInForString.java:26)
at freemarker.core.Expression.eval(Expression.java:78)
at freemarker.core.Expression.evalAndCoerceToString(Expression.java:82)
at freemarker.core.DollarVariable.accept(DollarVariable.java:40)
at freemarker.core.Environment.visit(Environment.java:312)
at freemarker.core.MixedContent.accept(MixedContent.java:62)
at freemarker.core.Environment.visitByHiddingParent(Environment.java:333)
at freemarker.core.IfBlock.accept(IfBlock.java:48)
at freemarker.core.Environment.visitByHiddingParent(Environment.java:333)
at freemarker.core.IteratorBlock Context.runLoop(IteratorBlock.java:159)atfreemarker.core.Environment.visitIteratorBlock(Environment.java:559)atfreemarker@H_430_301@.core.IteratorBlock.accept(IteratorBlock.java:67@H_157_403@)atfreemarker.c@H_403_435@ore.Environment.visit(Environment.java:312)atfreemarker.core.MixedContent.accept(MixedContent.java:62)atfreemarker.core.Environment.@H_301_727@visitByHiddingParent(Environment.java:333)atfreemarker.core.If@H_502_871@Block.accept(IfBlock.java:48)atfreemarker.core.Environment.visit(Environment.java:312)atfreemarker.core.MixedContent.accept(MixedContent.java:62)atfreemarker.core.Envi@H_404_1233@ronment.visit(Environment.java:@H_746_1301@312)atfreemarker.core.Environment.process(Enviro@H_696_1403@nment.java:290)at@H_403_1433@freemarker.template.Template.process(Template.java:312)atorg.apache.struts2.dispatcher.DefaultDispa@H_404_1645@tcherErrorHandler.handleErrorInDevMode(DefaultDispatcherErrorHandler.java:99)atorg.apache.struts2.dispatcher.DefaultDispatcherErrorHandler.handleError(DefaultDispatcherErrorHandler.java:57)atorg.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:909)atorg.apache.stru@H_404_2273@ts2.dispatcher@H_27_2301@.Dispatcher.serviceAction(Dispatcher.java:576)atorg.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)atorg.apache.struts2.dispatcher.ng.filt@H_404_2681@er.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilt@H_441_3011@e@H_946_3013@r@H_548_3015@(@H_790_3017@A@H_721_3019@pplicationFilterChain.java:235)atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)atorg.apa@H_121_3301@che.catalina.core.StandardWrap@H_301_3373@perValve.invok@H_766_3403@e(StandardWrapperValve.java:230)atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve@H_301_3653@.java:175)atorg.apache@H_403_3695@.catalina.core.StandardHostValve.invok@H_404_3785@e(StandardHostValve.java:128)atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)atorg@H_359_4033@.@H_347_4035@a@H_71_4037@p@H_898_4039@a@H_423_4041@c@H_962_4043@h@H_657_4045@e@H_462_4047@.@H_837_4049@catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)atorg.apache.@H_301_4245@catalina.connector.CoyoteA@H_924_4301@dapter.service(CoyoteAdapter.java:261)atorg.ap@H_306_4403@ache.coyote.http11.Http11Processor.p@H_502_4479@rocess(Http11Processor.java:844)atorg.apache.coyote.http11.Http11Protocol Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
at freemarker.ext.beans.SimpleMethod.unwrapArguments(SimpleMethod.java:53)
at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:72)
… 44 more

然后通过询问别人,知道了JsonConfig的jsonConfig.setExcludes()这个方法,简单的说,这个就是过滤掉不需要的属性,因此就可以用这个方法过滤掉对象中存在的一对多的关系

List userList = userService.findAll();
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExcludes(new String[]{“area”});
JSON json = JSONSerializer.toJSON(userList,jsonConfig);

setExcludes()里面的集合,就是你想要过滤掉的属性的名字。

原文链接:https://www.f2er.com/json/289543.html

猜你在找的Json相关文章