I am working on creating a web application using struts. I want to send a JSON error message as shown below when the request URL is not formed correctly.
{
"status": 409,
"code": 40924
"property": "aggregation",
"message": "aggregationId not specified.",
"moreInfo": "https://www.iiitb-swn.com/docs/api/errors/40924"
}
I already use the struts2-json plugin to serialize response objects using JSON. How should I send JSON error responses. I can think about how to do the same.
Use the error response object in the action class and explicitly specify all pairs of names whose names are explicitly specified
private Map<String, String> errorObject;
public String execute()
{
...
if (aggregationId == -1)
{
errorObject = new HashMap<>();
errorObject.put("status", "400");
errorObject.put("code", "40924");
...
return INPUT;
}
...
}
Then I could only handle serialization errorObjectin mine struts.xml.
I'm new to Struts and wondered if there is an established way to do this? Perhaps the one that uses the Struts structure better.