title: How to construct a custom JSON error response using the Struts 2 framework
body: <p>I am working on creating a web application using Struts 2. I want to send out JSON error response like the below when the request URL is not well formed:</p>
  <pre><code>{
    “status”: 409,
    &quot;code&quot;: 40924
    “property”: “aggregation”,
    “message”: “aggregationId not specified.”,
    “moreInfo”: “https://www.iiitb-swn.com/docs/api/errors/40924”
  } 
  </code></pre>
  <p>I am already using the struts2-json plugin for serializing response objects using JSON. How should I go about sending JSON error responses. I can think of the below way of doing the same.</p>
  <p>Use an error response object in the action class and set all name required name value pairs explicitly</p>
  <pre><code>private Map&lt;String, String&gt; errorObject;
  
  public String execute()
  {
      ...
      if (aggregationId == -1)
      {
          errorObject = new HashMap&lt;&gt;();
          errorObject.put(&quot;status&quot;, &quot;400&quot;);
          errorObject.put(&quot;code&quot;, &quot;40924&quot;);
          ...
          return INPUT;
      }
      ...
  }
  </code></pre>
  <p>I could then handle serializing only the <code>errorObject</code> in my <code>struts.xml</code>.</p>
  <p>I am wondering if there is an established way of doing this? Perhaps one which makes using of the Struts 2 framework better.</p>