前台传数组或list,后台接收方式

要传的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
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值