JSON応答を取得するためにSpringMVC3コードを実装しました(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;
}
}
春のメソッド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応答が作成されます。必要な形式のJSON応答を実現するにはどうすればよいですか?