要传的List<TScteRechargeCouponRelate>
对象如下:
public class TScteRechargeCouponRelate {
private Integer id;
private String mchntCd;
private Integer rechargeId;
private Integer couponId;
private String couponName;
}
TJfzwMerchantRecharge
对象如下:
public class TJfzwMerchantRecharge {
private Integer id;
private String mchntCd;
private List<TScteRechargeCouponRelate> couponRelates;
}
一、对象包装
用TJfzwMerchantRecharge 对象把List TScteRechargeCouponRelate 包装起来
/**
* @description: 1、对象包装
* @author: jushisi
*/
@RequestMapping("/testList1.action")
@ResponseBody
public ComRes testList1(TJfzwMerchantRecharge charge) throws IOException {
LogWriter.info("[/testList] param--->" + JSON.toJSONString(charge));
ComRes res = new ComRes();
try {
res = new ComRes(RspmsgConst.CODE_SUCESS, RspmsgConst.DESC_SUCESS);
}catch (Exception e) {
LogWriter.error("[/testList] error ---> ", e);
res = new ComRes(RspmsgConst.SYSTEM_ERROR_CODE, RspmsgConst.SYSTEM_ERROR_MSG);
}
return res;
}
postman模拟请求,请求成功:Content-Type: application/x-www-form-urlencoded;charset=UTF-8
二、json字符串转换
前端用JSON.stringify()
把List或复杂对角转成json串,controller用字符串接收,再用JSON工具类()转成对应的对象
/**
* @description: 2、json字符串转换
* @author: jushisi
*/
@RequestMapping("/testList2.action")
@ResponseBody
public ComRes testList2(String jsonStr) throws IOException {
LogWriter.info("[/testList] param--->" + jsonStr);
List<TScteRechargeCouponRelate> couponRelates = JSON.parseArray(jsonStr, TScteRechargeCouponRelate.class);
ComRes res = new ComRes();
try {
res = new ComRes(RspmsgConst.CODE_SUCESS, RspmsgConst.DESC_SUCESS);
}catch (Exception e) {
LogWriter.error("[/testList] error ---> ", e);
res = new ComRes(RspmsgConst.SYSTEM_ERROR_CODE, RspmsgConst.SYSTEM_ERROR_MSG);
}
return res;
}
postman模拟请求,请求成功:Content-Type: application/x-www-form-urlencoded;charset=UTF-8
三、@RequestBody注解
/**
* @description: 3、@RequestBody注解
* @author: jushisi
*/
@RequestMapping(value="/testList3.action")
@ResponseBody
public ComRes testList3(@RequestBody List<TScteRechargeCouponRelate> couponRelates) throws IOException {
LogWriter.info("[/testList] param--->" + JSON.toJSONString(couponRelates));
ComRes res = new ComRes();
try {
res = new ComRes(RspmsgConst.CODE_SUCESS, RspmsgConst.DESC_SUCESS);
}catch (Exception e) {
LogWriter.error("[/testList] error ---> ", e);
res = new ComRes(RspmsgConst.SYSTEM_ERROR_CODE, RspmsgConst.SYSTEM_ERROR_MSG);
}
return res;
}
postman模拟请求,请求成功:Content-Type: application/json;charset=UTF-8