I did a lesson about Spring Boot
and it works perfectly. But what if I want to return a set of objects ? I tried doing this but it doesn\'t work. How can I do i
Let Say we have list of CarDetails Pojo and we want to return them back
@RestController
public class CarDetailController {
@GetMapping("/viewAllCarDetailList")
public List retrieveAllCarDetails() {
List contacts = new ArrayList();
CarDetail objt = new CarDetail();
objt.setCarModel("hyundai");
objt.setSubModel("I10");
CarDetail objt2 = new CarDetail();
objt2.setCarModel("hyundai");
objt2.setSubModel("I20");
contacts.add(objt);
contacts.add(objt2);
return contacts;
}
}
public class CarDetails {
private String carModel;
private String subModel;
// Will haave Setter getter and hash code equls method
//and constructor
}
This JSON will be output:-
[
{
"carModel": "hyundai",
"subModel": "I10"
},
{
"carModel": "hyundai",
"subModel": "I20"
}
]