Struts2Action使用插件返回json时报错问题

来源:互联网 发布:战地4优化怎么样 编辑:程序博客网 时间:2024/06/05 15:46

     @JSON(serialize=false)     public AuthorityService getAuthorityService() {         return authorityService;     }

Struts2Action使用插件返回json时报如下错误:

<span style="font-size:18px;">org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.IllegalAccessException: Class org.apache.struts2.json.JSONWriter can not access a member of class org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper with modifiers "public"</span>


主要原因:struts会将action中定义的一些变量序列化转换成json格式,需要调用对象的一系列get方法(例子中调用authorityServiceauthorities的get方法),并调用以上两个变量的成员变量的get方法将其内容组成json格式。但是在序列化authorityService时,由于其成员变量中含有含有接口所以会报错。


解决方案:

1)修改配置文件:指定序列化的根节点,这样data就是从authorities的根节点以下的数据,不需要用data.authorities

     <package name="super_admin" extends="json-default" namespace="/superadmin">         <action name="allAuthorities" class="edu.bjfu.action.AuthoritiesAction">             <result type="json"><param name="root">authorities</param></result>         </action>     </package>

2) 修改java代码:让编译器不对authorityService序列化

     @JSON(serialize=false)     public AuthorityService getAuthorityService() {         return authorityService;    }

3)此外貌似可以通过配置文件的excude和include来指定需要序列化的对象,我没有自己去试!

4)可以去掉不必要的get方法也可以解决

转载自:http://www.cnblogs.com/xiaoyaorensheng/archive/2013/01/02/2842302.html

0 0
原创粉丝点击