我的JSON對象的JSON體發送到後端時,設置@RequestBody看起來是這樣的:發送包含對象的對象和數組
{
"reportAccess":{
"report":{
"id":70,
"reportName":"Test Rapport",
"created":"2017-06-01",
"description":"Test report",
"modifiedDate":"2017-06-01T12:10:00.000+0000",
"modifiedBy":"111111"
},
"accessGroup":{
"id":1,
"accessName":"Administrator"
},
"validFromDate":"2017-07-24",
"validToDate":"2017-07-30"
},
"users":[
{
"id":111111,
"employeeNumber":"123456",
"firstName":"Test1",
"lastName":"Test11",
"windowsId":"t1234567"
},
{
"id":222222,
"employeeNumber":"7891011",
"firstName":"Test2",
"lastName":"Test22",
"windowsId":"t7891011"
}
]
}
的前端後撥打電話:
function addAccessForMultipleAgents(access, users) {
var holderWrapper = {
reportAccess: access,
users: users
};
return utils.post('/api/v1/reportAccesses/all', holderWrapper);
}
後端,我有一個包裝對象,看起來像這樣:
HolderWrapper.java
public class HolderWrapper {
private List<User> users;
private ReportAccessCustom reportAccess;
public HolderWrapper() {
}
public HolderWrapper(List<User> users, ReportAccessCustom reportAccess) {
this.users = users;
this.reportAccess = reportAccess;
}
public List<User> getUsers() {
return users;
}
public ReportAccessCustom getReportAccess() {
return reportAccess;
}
@Override
public String toString() {
return "HolderWrapper{" +
"users=" + users +
", reportAccess=" + reportAccess +
'}';
}
}
ReportAccessCustom.java
public class ReportAccessCustom {
private Report report;
private AccessGroup accessGroup;
private Date validFromDate;
private Date validToDate;
public Report getReport() {
return report;
}
public AccessGroup getAccessGroup() {
return accessGroup;
}
public Date getValidFromDate() {
return validFromDate;
}
public Date getValidToDate() {
return validToDate;
}
}
並處理該POST請求的代碼:
@RequestMapping(value = "", method = RequestMethod.POST)
public List<ReportAccess> createMultipleAccesses(@RequestBody Map<String, Object> holderWrapper) {
List<ReportAccess> response = new ArrayList<>();
HolderWrapper hw = new HolderWrapper((ArrayList<User>) holderWrapper.get("users"), (ReportAccessCustom) holderWrapper.get("reportAccess"));
List<User> users = hw.getUsers();
ReportAccessCustom reportAccessCustom = hw.getReportAccess();
//rest of code..
return response;
}
這一切給我留下的錯誤消息:
任何幫助,爲什麼這失敗woul不勝感激。
你爲什麼不定義參數作爲'HolderWrapper holderWrapper'? – Thomas
@Thomas'@RequestBody HolderWrapper holderWrapper'你的意思是?我試過這樣結束了'NullPointerException'。那麼我應該如何建模對象前端? –