我已经实现了一个 spring mvc 3 代码来获取 JSON 响应(在 jackson mapper 的帮助下)
@RequestMapping(value = "/getallroles", method = RequestMethod.GET)
@ResponseBody
public JsonJtableResponse1 getAllRoles(){
List<Role> roleList = testService.getAllRoles();
JsonJtableResponse1 jstr = new JsonJtableResponse1("OK",roleList);
return jstr;
}
JSON 响应对象是这样的。
public class JsonJtableResponse1 {
private String Result;
private List<Role> Records;
public JsonJtableResponse1(String Result) {
this.Result = Result;
}
public JsonJtableResponse1(List<Role> Records) {
this.Records = Records;
}
public JsonJtableResponse1(String Result, List<Role> Records) {
this.Result = Result;
this.Records = Records;
}
public String getResult() {
return Result;
}
public void setResult(String Result) {
this.Result = Result;
}
public List<Role> getRecords() {
return Records;
}
public void setRecords(List<Role> Records) {
this.Records = Records;
}
}
从 spring 方法 getAllRoles() 返回的 JSON 是
{"result":"OK","records":[
{"custId":"1","name":"aaa","birthYear":"1982","employer":"XX","infoAsOfDate":"20130110","disabled":"true"},
{"custId":"2","name":"bbb","birthYear":"1982","employer":"YY","infoAsOfDate":"20130111","disabled":"true"},
{"custId":"3","name":"ccc","birthYear":"1982","employer":"XX","infoAsOfDate":"20130108","disabled":"false"},
{"custId":"4","name":"ddd","birthYear":"1981","employer":"TT","infoAsOfDate":"20130107","disabled":"true"}
]}
我需要 JSON 作为 - [注意两个元素中的大写 R]
{"Result":"OK","Records":[ ....................
..............................................
]}
使用 Jakson 映射器创建 JSON 响应时考虑了对象的 getter/setter 名称。如何实现所需的 JSON 响应格式?