Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include parent class fields in json response using struts 2 json plugin

Tags:

json

struts2

I have following implementation

public abstract class BaseAcion extends ActionSupport {
    private String result;
    private String message;

    //getters, setters
}

public class MyAction extends BaseAction {
    private String myFirstField;
    private String mySecondField;

    public String execute() {
         ...
         myFirstField = "someValue";
         mySecondField = "someOtherValue";
         ...
         result = SUCCESS;
         message = "Some message here";
         ...
         return result;
    }

    //methods, getters, setters
}

I have used struts2-json plugin, action mapping is

<package name="my-package" namespace="/" extends="json-default" >
    <action name="myAction" class="MyAction">
        <result type="json"></result>
    </action> 
</package>

The response that I receive is something like this.

{
    "myFirstField":"someValue",
    "mySecondField":"someOtherValue"
}

I want to get "result" and "message" fields in response as well.

How can I include BaseAction fields in json response?

like image 405
Bilal Mirza Avatar asked Apr 10 '12 05:04

Bilal Mirza


1 Answers

By default properties defined on base classes of the "root" object won't be serialized, to serialize properties in all base classes (up to Object) set "ignoreHierarchy" to false in the JSON result:

Something like

<result type="json">
  <param name="ignoreHierarchy">false</param>
</result>

For details refer to the JSON plugin documentation

  • JSONPlugin-BaseClasses
like image 157
Umesh Awasthi Avatar answered Oct 28 '22 04:10

Umesh Awasthi