'How to create a costum JSON response in Spring Java?

Currently, I am trying to return a costum created response from my REST API. My goal is to create the following response.

{
   "status": "Success",
   "message": "More details here",
   "data": {
      "records": [
         {
            "record": "Lorem ipsum",
            "code": 0,
            "errors": null
         },
         {
            "record": "Lorem ipsum",
            "code": 1,
            "errors": [
               "First error",
               "Second error"
            ]
         }
      ]
   }
}

My API endpoint looks like the following.

@PostMapping(value="/my-url", 
   consumes=MediaType.APPLICATION_JSON_VALUE, 
   produces=MediaType.APPLICATION_JSON_VALUE)
public Response check_records(@RequestBody(required=true) Records records) {
   // Do Stuff
}

The 'Response' class:

public class Response {
   private String status;
   private String message;
   private Map<String, List<RecordResponse>> data;
   
   // Default getter / setter
}

The 'RecordResponse' class:

public class RecordResponse {
   private String record;
   private Integer code;
   private String[] errors;

The response itself is returning anything but JSON. Neither way it is returning an error or returns the object reference in the json:

{
   "status": "Success",
   "message": "More details here",
   "data": {com.example.restservice.data@2071af5d}
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source