I have a Spring 3 controller that returns a JSON object. I am using @ResponseBody annotation, and jackson-mapper-asl jar, with which the Spring will handle the JSON conversion automatically. The 3 return statements returns different JSON format. Can this be handled by modifying the return type of getPersonDetails method with Object or is there a better way.
@RequestMapping(value="/test", method=RequestMethod.GET)
public @ResponseBody List<Person> getPersonDetails() {
List<Person> listPerson = null;
try {
// Call to service and get the list of Person
listPerson = getPersonList();
if(CollectionUtils.isNotEmpty(listPerson)) {
// Return JSON object
//{"Name":"XYZ", "Age":25}
} else {
// Return JSON object
//{"InformationMessage":"No data found."}
}
} catch(final Exception e) {
// Return JSON object
// {"ExceptionMessage":"Exception in controller."}
}
}