4

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:

{
  “status”: 409,
  "code": 40924
  “property”: “aggregation”,
  “message”: “aggregationId not specified.”,
  “moreInfo”: “https://www.iiitb-swn.com/docs/api/errors/40924”
} 

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.

Use an error response object in the action class and set all name required name value pairs explicitly

private Map<String, String> errorObject;

public String execute()
{
    ...
    if (aggregationId == -1)
    {
        errorObject = new HashMap<>();
        errorObject.put("status", "400");
        errorObject.put("code", "40924");
        ...
        return INPUT;
    }
    ...
}

I could then handle serializing only the errorObject in my struts.xml.

I am wondering if there is an established way of doing this? Perhaps one which makes using of the Struts 2 framework better.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Abhijith Madhav
  • 2,748
  • 5
  • 33
  • 44
  • 1
    For the status and error codes, [take a look at this](http://stackoverflow.com/a/17235529/1654265). For the other properties, there is nothing out-of-the-box, just create a custom object and [set it as root](http://stackoverflow.com/a/17149414/1654265), or pick up only the parameters you need [by including them in the serialization process](http://struts.apache.org/release/2.3.x/docs/json-plugin.html#JSONPlugin-Includingproperties), or [take a look at more fanciful stuff](http://stackoverflow.com/a/4035455/1654265). – Andrea Ligios Nov 10 '14 at 12:55

2 Answers2

3

Struts2 actionErrors, fieldErrors provided by the ActionSupport. You can fill action errors or they are produced by the validation interceptor. For example

addFieldError("aggregation", “aggregationId not specified.”);
addFieldError("moreInfo", “https://www.iiitb-swn.com/docs/api/errors/40924”);

Then return json result as a response.

<result name="input" type="json">
  <param name="statusCode">409</param>
  <param name="errorCode">40924</param>
  <param name="ignoreHierarchy">false</param>
  <param name="includeProperties">^actionErrors.*,^fieldErrors.*</param>
</result> 
Roman C
  • 49,761
  • 33
  • 66
  • 176
1

In this case I preferred to add the "fieldErrors" property as root object of the result, instead of filtering with regular expressions.

So, I added this to the action configuration (in this case with annotations)

@Result(name="input", type="json", params={"root","fieldErrors"})

And in the ajax configuration, under the success result, I used the returned JSON as

success : function(fieldErrors, textStatus, jqXHR) {
        for (var property in fieldErrors) {
            if (fieldErrors.hasOwnProperty(property)) {
                var this_field_err = fieldErrors[property];
                $('#submit_result').append(property+" error");
                for(var ix=0; ix<this_field_err.length; ix++) {
                    $('#submit_result').append(this_field_err[ix]);
                    $('#submit_result').append("<br>"); 
                }
            }
        }
    }

this adds to the #submit_result div I have the page

username error: Username must be at least 6 charachers long
password error: Password msut be at least 8 charachers long 
Jose Ospina
  • 2,097
  • 3
  • 26
  • 40
  • The problem with this approach is that you cant include additional properties if you need them. Not a good thing, so its not recommended – Jose Ospina Feb 07 '16 at 11:25