我有一個Spring 3控制器,它返回一個JSON對象。我使用@ResponseBody註釋和jackson-mapper-asl jar,Spring會使用它自動處理JSON轉換。 3個返回語句返回不同的JSON格式。這可以通過用Object修改getPersonDetails方法的返回類型來處理,還是有更好的方法。Spring @ResponseBody JSON
@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."}
}
}