Monday, 20 January 2014

Struts2 json result on jquery grid

As you all know struts2 supports JSON type results.When returning json result in struts2, many times I used to get the error
Class org.apache.struts2.json.JSONWriter can not access a member of class with modifiers "public"...

Usually this error will not be shown on the screen. To find the error, you should use any plugins like httpfox of firefox.
After a long search with many tools I got the reason for the issue and I am posting it here, as it can save many of your's time.

So the reason for this issue is,
When we are getting the json result from the struts request, the plugin tries to serialize the entire object graph and converting it into json.
To avoid this,use the below code and exclude the parameters which you don't want to serialize.
Generally the session variables declared in the classes should not be serialized for which i was getting the error. So mention the variable name in excludeproperties as follows
<action name="XXXAction" class="yourxxxclass" method="xxxmethod">
<result name="success" type="json" >
<param name="excludeProperties">
xxx(variable from yourxxxclass which you don't want to serialize)
</param>
</result>
</action>

No comments:

Post a Comment