0

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());

  • 1
    You need to use a library like GSON to convert your data to json. – Nicholas K Mar 17 '20 at 10:42
  • 2
    Why don't you define your error object ? Then use a parsing / serializing library of json like GSON or Jackson – Pievis Mar 17 '20 at 10:44
  • Any example? and how would you do it? I'm a bit lost –  Mar 17 '20 at 10:45
  • very [easy](https://stackoverflow.com/questions/15786129/converting-java-objects-to-json-with-jackson) to [find](https://stackoverflow.com/questions/16377754/parse-json-file-using-gson) – HomeIsWhereThePcIs Mar 17 '20 at 10:46
  • @JS4 there are plenty of examples out there. This site is not for suggestions but for solving well defined problems. – lainatnavi Mar 17 '20 at 10:47
  • 1
    Does this answer your question? [Is there a way to implement an array of LinkedHashMap?](https://stackoverflow.com/questions/4128923/is-there-a-way-to-implement-an-array-of-linkedhashmap) – Hooman.AS Mar 17 '20 at 10:59
  • @HomeIsWhereThePcIs but those examples gave me a string always with this format "[{\"code\":500,\"message\":\"error\"}]". Maybe I'm doing something wrong? what i need is an array of objects. –  Mar 17 '20 at 11:48

3 Answers3

0

Your JSON shows a list of error objects, each with a code field and a message field.

You should serialise your data into a List of these objects and then place this into your arrayObj.

0

You can use https://code.google.com/archive/p/json-simple/

try
    {
        JSONObject customResponse=new JSONObject();
        JSONArray errorArray=new JSONArray();
        customResponse.put("errors", errorArray);
        JSONObject errorsObject=new JSONObject();
        errorsObject.put("code", "500");
        errorsObject.put("message", "Internal Server Error");
        errorArray.put(errorsObject);

        //customResponse will have format.....


    }
    catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
-1

There are multiple ways to describe it, which depend on requirement.

In usual cases error code should be one with multiple (similar type)messages.

For Example :- Request body contains multiple invalid values. In this scenario response will have code as 400 while message will be for respective field(s).

You may return Object as shown below and build it as needed.

public class ErrorBuilder{
  private int errorCode;
  private List<String> errorMessages;
}
Swarit Agarwal
  • 1,916
  • 19
  • 29