Home > database >  How to make spring returns json array not a json object?
How to make spring returns json array not a json object?

Time:01-23

I am returning a list from a method in a spring controller and the json output is json object not a json array?

@GetMapping(value = "/students")
public List<Student> getStudents() {

    List<Student> list = service.getStudents();

    return list;
}

What I get is a json object containing the json array. { "students":[ { "name":"xyz" }, { "name":"xyz" }, { "name":"xyz" } ] }

What I need a flat json array like this.

[ { "name":"xyz" }, { "name":"xyz" }, { "name":"xyz" } ]

CodePudding user response:

return ResponseEntity:

@GetMapping(value = "/students")
public ResponseEntity<List<Student>> getStudents() {
    List<Student> list = service.getStudents();
    return new ResponseEntity<>(list, HttpStatus.OK);
}
  • Related