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

I have the 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 used struts2-json plugin, action mapping

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

The answer I get looks something like this.

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

I also want to get the "result" and "message" fields.

How to include BaseAction fields in json response?

+5
source share
1 answer

By default, properties defined in the base classes of the “root” object will not be serialized, to serialize properties in all base classes (before Object), set “ignoreHierarchy” to false as a result of JSON:

Sort of

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

See the JSON plugin documentation for more details.

+11
source

All Articles