日期:2014-05-16  浏览次数:20341 次

利用struts2的Ajax返回JSON时报错
今天做了SSH2框架下的Ajax从后台去处List返回前台,数据取成功了,前台也可以显示,但总会报错如下:java.lang.IllegalAccessException: Class com.googlecode.jsonplugin.JSONWriter can not access a member of class org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper with modifiers "public"

ERROR [http-8080-3] [default].invoke(253) | Servlet.service() for servlet default threw exception
java.lang.IllegalAccessException: Class com.googlecode.jsonplugin.JSONWriter can not access a member of class org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper with modifiers "public"
	at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
	at java.lang.reflect.Method.invoke(Method.java:588)
	at com.googlecode.jsonplugin.JSONWriter.bean(JSONWriter.java:222)
	at com.googlecode.jsonplugin.JSONWriter.process(JSONWriter.java:159)
	at com.googlecode.jsonplugin.JSONWriter.process(JSONWriter.java:159)
	at com.googlecode.jsonplugin.JSONWriter.value(JSONWriter.java:125)
	at com.googlecode.jsonplugin.JSONWriter.write(JSONWriter.java:93)
	at com.googlecode.jsonplugin.JSONUtil.serialize(JSONUtil.java:96)
	at com.googlecode.jsonplugin.JSONResult.execute(JSONResult.java:175)
	at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:343)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
	at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:213)
:188)
	at com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:118)
	at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter

上网查了好久,说是因为有类型不能序列化,所以爆出此错,网上说的两个解决方法:
1. 将不能序列化的方法的get方法删去
2. 在此get方法前加入注解@JSON(serialize=false)强制其不进行序列化。
在我用的SSH2中,显然不能用第一种方式,因为报错的,就是因为一些application.xml中Spring依赖注入的一些业务逻辑类不能序列化,利用DataManager,MessageManager这样一些Manager,删去get方法,去new XXXManager显然将Spring带来的方便和好处抛弃了,所以:
找到当前action在spring配置文件中以来的业务逻辑类,然后在action类中找到这些所对应的get方法,如public XXManager getXXXManager(){}在其前面加上@JSON(serialize=false)即可,注意set不用加
我的异常网推荐解决方案:Servlet.service() for servlet default threw exception,http://www.aiyiweb.com/eclipse/181756.html