當springboot寫后端接口時需要傳入一個多層嵌套的大類UpdateAll(或者object)時,需要進行每一層object到類的轉化,傳入的前端參數形式為:
{
"before":{
"country": "中國",
"nation": "漢族",
"csplace": "北京"
},
"now":{
"country": "美國",
"nation": "漢族",
"csplace": "甘肅"
},"others":
{
"cUpdateOperate": "2",
"cUpdateType": "0",
"cUpdateOperateId": "yanjiangyi"
}
}
處理方法:
1、定義第一層大類UpdateAll()
@Data
@TableName("TABLE_UPDATE")
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="TABLE_UPDATE對象", description="basicinformation")
public class UpdateAll {
@ApiModelProperty("修改之前的信息")
private Object before; #對於第一層大類中包含的后續不確定具體屬性字段的第二層類,我們需要定義為object
@ApiModelProperty("修改之后的信息")
private Object now;
@ApiModelProperty("其余信息")
private UpdateOthers others; #對於第一層大類中包含的后續確定具體字段的第二層類,我們需要定義為具體類名即可
public UpdateAll(){} //添加無參數構造函數,一定不要忘記
}
2、將不同的第二層類或者object對象根據類型都需要統一轉化為具體類
(1)object的轉化方式:
// // 將傳過來的before,now和othes三個object轉為大類
// String objectstring = JSONArray.toJSONString(object);
// UpdateAll updateAll = JSONObject.parseObject(objectstring, UpdateAll.class);
String before = JSONArray.toJSONString(updateAll.getBefore());
UpdateBasicInformation updatebefore = JSONObject.parseObject(before, UpdateBasicInformation.class)
String now = JSONArray.toJSONString(updateAll.getNow());
UpdateBasicInformation updatenow = JSONObject.parseObject(now, UpdateBasicInformation.class);
(2)具體類的轉化方式:
UpdateOthers updateOthers=updateAll.getOthers(); //直接利用get方法的方式獲得的直接就是類
3、springboot后端接口傳入的時候需要定義為具體的updateALL大類,用具體類的方式進行傳參(也可以使用object的方式傳參,之后進行轉換)
@AutoLog(value = "個人檔案信息編輯")
@ApiOperation(value = "個人檔案信息編輯", notes = "個人檔案信息編輯")
@PostMapping(value = "/save1")
public Result<Object> save(@RequestBody UpdateAll updateAll) {
Result<Object> result = new Result<Object>();
// // 將傳過來的before,now和othes三個object轉為大類
// String objectstring = JSONArray.toJSONString(object);
// UpdateAll updateAll = JSONObject.parseObject(objectstring, UpdateAll.class);
// String others = JSONArray.toJSONString(updateAll.getOthers());
// UpdateOthers updateOthers = JSONObject.parseObject(others, UpdateOthers.class);
// UpdateOthers updateOthers = (UpdateOthers)updateAll.getOthers(); 強制轉化類的方式,一般不推薦使用,可能存在問題。