title: How to bind JSON to Java object in Struts2 using struts2-json-plugin
body: <p>I want to deserialize (bind) JSON to java object. How to do it in Struts2?</p>
  
  <p>I am trying to do it with struts2-json-plugin as you can see in code below, but sent JSON from frontend is not binding to my java object. Could you help me, please how to make this code to work correctly? </p>
  
  <p>Please take a look at my Action class, I am not sure if I'm handling JSON correctly in this Action, or maybe I missed something?</p>
  
  <p><strong>JSON which I am trying to bind:</strong></p>
  
  <pre><code>{"data":[
      {"active":true,"color":"orange","date":"2008-01-01","id":1,"name":"Chris"},
      {"active":false,"color":"blue","date":"2013-03-03","id":2,"name":"Kate"},
      {"active":true,"color":"black","date":"2013-05-03","id":3,"name":"Blade"},
      {"active":false,"color":"yellow","date":"2013-01-01","id":4,"name":"Zack"}]
  }
  </code></pre>
  
  <p><strong>Sending JSON by Ajax:</strong></p>
  
  <pre><code>$.ajax({
    url: "../json/saveJSONDataAction.action",
    data: {"data": handsontable.getData()}, //returns all cells' data
    dataType: 'json',
    type: 'POST',
    success: function (res) {
      if (res.result === 'ok') {
        $console.text('Data saved');
      }
    }
  });
  </code></pre>
  
  <p><strong>Receiving JSON in Struts2:</strong></p>
  
  <p><strong>I can reach execute() method in debug, but unfortunately, the <code>data</code> field is always null. What should I do to make this field filled with data from JSON?</strong> Is the JSON in correct format to bind to <code>List&lt;Report&gt; data</code>?</p>
  
  <pre><code>@ParentPackage("json-default")
  @Action(value="saveJSONDataAction")
  @Result(type="json")
  public class JSONSaveAction extends ActionSupport {
      private static final long serialVersionUID = 1L;
  
      private List&lt;Report&gt; data;
  
      public JSONSaveAction(){
      }
  
      public String execute() {
          try {
              System.out.println(data);
          } catch (Exception e) {
              e.printStackTrace();
          }
          return NONE;
      }
  
      public List&lt;Report&gt; getData() {
          return data;
      }
  
      public void setData(List&lt;Report&gt; data) {
          this.data = data;
      }
  }
  </code></pre>
  
  <p><strong>Report class:</strong></p>
  
  <pre><code>public class Report {
      private int id;
      private String name;
      private boolean active;
      private String date;
      private String color;
  
      //getters and setters
  }
  </code></pre>
  
  <p><strong>struts.xml:</strong></p>
  
  <p>As you can see here I've added <code>&lt;interceptor-ref name="json"&gt;</code> with <code>&lt;param name="enableSMD"&gt;true&lt;/param&gt;</code>. Whole config below:</p>
  
  <pre><code>&lt;struts&gt;
  &lt;constant name="struts.action.extension" value="action,pdf" /&gt;
  &lt;constant name="struts.i18n.reload" value="true" /&gt;
  &lt;constant name="struts.configuration.xml.reload" value="true" /&gt;
  &lt;constant name="struts.custom.i18n.resources" value="i18n/ap,application" /&gt;
  &lt;constant name="struts.date.format" value="yyyy-MM-dd" /&gt;
  &lt;constant name="struts.serve.static" value="true" /&gt;
  &lt;constant name="struts.serve.static.browserCache" value="false" /&gt;
  
  &lt;package name="default" namespace="/ftl" extends="json-default"&gt;
  
      &lt;result-types&gt;
         &lt;result-type name="rethrowException" class="com.myhome.commons.util.ExceptionRethrowResult" /&gt;
         &lt;result-type name="poi-excel" class="com.myhome.commons.util.PoiExcelResult"/&gt;
      &lt;/result-types&gt;
  
      &lt;interceptors&gt;
          &lt;interceptor name="businessException" class="com.myhome.commons.exception.BusinessExceptionInterceptor"&gt;&lt;/interceptor&gt;
          &lt;interceptor-stack name="defaultStack"&gt;
              &lt;interceptor-ref name="exception" /&gt;
              &lt;interceptor-ref name="alias" /&gt;
              &lt;interceptor-ref name="servletConfig" /&gt;
              &lt;interceptor-ref name="i18n" /&gt;
              &lt;interceptor-ref name="chain" /&gt;
  
              &lt;interceptor-ref name="scopedModelDriven" /&gt;
              &lt;interceptor-ref name="modelDriven" /&gt;
              &lt;interceptor-ref name="fileUpload"&gt;
                  &lt;param name="maximumSize"&gt;10485760&lt;/param&gt;
              &lt;/interceptor-ref&gt;
              &lt;interceptor-ref name="checkbox" /&gt;
              &lt;interceptor-ref name="staticParams" /&gt;
              &lt;interceptor-ref name="params"&gt;
                  &lt;param name="excludeParams"&gt;dojo\..*&lt;/param&gt;
              &lt;/interceptor-ref&gt;
              &lt;interceptor-ref name="json"&gt;
                  &lt;param name="enableSMD"&gt;true&lt;/param&gt;
              &lt;/interceptor-ref&gt;
              &lt;interceptor-ref name="prepare" /&gt;
              &lt;interceptor-ref name="conversionError" /&gt;
              &lt;interceptor-ref name="businessException" /&gt;
              &lt;interceptor-ref name="validation"&gt;
                  &lt;param name="includeMethods"&gt;save,search&lt;/param&gt;
              &lt;/interceptor-ref&gt;
              &lt;interceptor-ref name="workflow"&gt;
                  &lt;param name="includeMethods"&gt;save,search&lt;/param&gt;
              &lt;/interceptor-ref&gt;
              &lt;interceptor-ref name="tokenSession"&gt;
                  &lt;param name="includeMethods"&gt;save&lt;/param&gt;
              &lt;/interceptor-ref&gt;
          &lt;/interceptor-stack&gt;
  
      &lt;/interceptors&gt;
      &lt;default-interceptor-ref name="defaultStack"/&gt;
  
      &lt;global-results&gt;
          &lt;result name="exception" type="chain"&gt;
              &lt;param name="actionName"&gt;exception&lt;/param&gt;
              &lt;param name="namespace"&gt;/&lt;/param&gt;
          &lt;/result&gt;
          &lt;result name="rethrowException"&gt;/applicationAccessDeniedPage.jsp&lt;/result&gt;       
          &lt;result name="applicationAccessDenied"&gt;/applicationAccessDeniedPage.jsp&lt;/result&gt;
          &lt;result name="unavailableResource"&gt;/unavailableResource.jsp&lt;/result&gt;        
          &lt;result name="pessimisticLock"&gt;/pessimisticLock.jsp&lt;/result&gt;        
          &lt;result name="goto-crud" type="redirect"&gt;/crud/index.action&lt;/result&gt;
          &lt;result name="goto-dict" type="redirect"&gt;/dictionaries/index.action&lt;/result&gt;
          &lt;result name="reportXls" type="poi-excel"&gt;
              &lt;param name="contentDisposition"&gt;attachment; filename="${resultFileName}"&lt;/param&gt;
              &lt;param name="excelWorkbook"&gt;workbook&lt;/param&gt;
          &lt;/result&gt;
  
      &lt;/global-results&gt;
      &lt;global-exception-mappings&gt;
          &lt;exception-mapping exception="com.myhome.ap.service.exception.AuthorizationFailedException" result="rethrowException"/&gt;
          &lt;exception-mapping exception="com.myhome.ap.service.exception.ApplicationAccessDeniedException" result="applicationAccessDenied"/&gt;
          &lt;exception-mapping exception="org.hibernate.ObjectNotFoundException" result="unavailableResource" /&gt;
          &lt;exception-mapping exception="com.myhome.ap.service.exception.model.EntityHasBeenDeletedException" result="unavailableResource" /&gt;
          &lt;exception-mapping exception="com.myhome.ap.service.exception.PessimisticLockingException" result="pessimisticLock" /&gt;
          &lt;exception-mapping exception="java.lang.Exception" result="exception"/&gt;
       &lt;/global-exception-mappings&gt;
  
      &lt;action name="version" class="com.myhome.ap.web.action.VersionAction" /&gt;
  
  &lt;/package&gt;
  &lt;/struts&gt;
  </code></pre>
  
  <p>What am I doing wrong? Can you suggest me some good examples/tutorial how to do deserialization from JSON to Java in Struts2, because I cannot find even one correct full example with JSON deserialization in Struts2, specially example of Action code which will receive the JSON and bind it to Java. </p>
  
  <p>I am new in Struts at all, so that's why I have problems to understand some issues and flow, for example how to receive and handle JSON in Action. There are examples for serialization with struts2-json-plugin, but with this subject I had no trouble. Please help me...</p>