我一直在處理一個控制器,我想在其中返回json字符串作爲響應。但問題是,我想在serialize/deserialize期間更改一些字段名稱,但我不想在實體對象上使用醜陋的註釋。JSON在Spring MVC上使用不同的字段名序列化
比方說
@Controller
@RequestMapping("/kfc/brands")
public class JSONController {
@RequestMapping(value="{name}", method = RequestMethod.GET)
public @ResponseBody Shop getShopInJSON(@PathVariable String name) {
Shop shop = new Shop();
shop.setName(name);
shop.setStaffNames(new String[]{"mkyong1", "mkyong2"});
return shop;
}
}
public class Shop {
String name;
String staffNames[];
String location;
//getter and setter methods
}
我要控制器返回staffNames as staff_names
,location as address
不使用任何註釋。
我認爲必須有一個自定義對象映射器結構,但找不到合適的示例。我在序列化代碼中手動設置字段名稱沒有問題。
PS:從mkyong