I'm new with java and I have this doubt. I'm making a custom response in a Rest API and I want to introduce an array of errors with this format.
"http_code": 500,
"status": "INTERNAL_SERVER_ERROR",
"info": "Internal Server Error",
"errors": [
{
"code": 500,
"message": "Internal Server Error"
}
]
But I don't know how to do it on JAVA. My code until now it's something like this:
LinkedHashMap<String, Object> arrayObj = new LinkedHashMap<String, Object>();
arrayObj.put("code", 500);
arrayObj.put("message", "Internal Server Error");
List<> errors = new ArrayList<>(); //This part is where I don't know what to do
// Should I put a LinkedHashMap as output result?
LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
map.put("http_code", 500);
map.put("status", "INTERNAL_SERVER_ERROR");
map.put("info", "Internal Server Error");
map.put("errors", errors); // in this part would introduce the array of objects
I am not able to know how to do it, because if I put Linkedhasmap as a result, there is no method that returns the object as I want. Thanks in advance.
EDIT 2:
Finally, I resolved the puzzle. Here's the code:
JSONArray errorArray = new JSONArray();
JSONObject errorsObject= new JSONObject();
errorsObject.put("code", 500);
errorsObject.put("message", "Internal Server Error");
errorArray.put(errorsObject.toMap());
LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
map.put("http_code", 500);
map.put("status", "INTERNAL_SERVER_ERROR"));
map.put("info", info);
map.put("errors", errorArray.toList());