Search code examples
javaspring-bootresponse-entity

ResponseEntity for Generic Object not giving empty response


When returning the response from controller in ResponseEntity for class B. But it returns empty json response. controller method is below:

 @PostMapping("/test")
    public ResponseEntity testSave(@Valid @RequestBody ReqData req, BindingResult result)
    {
        B response;
        if(result.hasErrors())
        {
            response=generateBadRequestResponse(result);//response variable of B type holds value in its fields, but not sending in response
            return ResponseEntity.ok(response); //getting empty json in postman response
        }

 
        return  ResponseEntity.ok(response);
    }

Whereas Class B is declared like this:

public class B <T> implements Serializable {

    private String message;
    private T body;
    private List<T> errorList= new ArrayList<>();
   
    //omitted unnecessary code for brevity
}

I tested the api from postman.


Solution

  • We need more details, a few scenarios it can happen:

    1. generateBadRequestResponse returns null
    2. no getters on B