Java后端接收Get、Post方式传值

Get方式传递数组与集合, Java后端接收

  1. Get方式传递
/**
 * @param Integer[] ids or List<Integer> list
 * @author code.Map
 * @version 创建时间:2021年3月9日 下午10:02:24
 * 请求地址: http://localhost:8888/Company/GetTransmit?list=4&list=2
 * 返回参数: [4,2]
 */
@GetMapping("/GetTransmit")
@ResponseBody
public List<Integer> GetTransmit(@RequestParam List<Integer> list) {
	List<Integer> varList = new ArrayList<>();
	if (null != list) {
		list.forEach(key -> varList.add(key));
		return varList;
	}
	return varList;
}

  1. Post方式传递, Java后端接收
/**
 * @param MapVo mapVo
 * @author code.Map
 * @version 创建时间:2021年3月9日 下午10:12:58
 * 请求地址: http://localhost:8888/Company/PostTransmit {"list": [2,4]}
 * 返回参数: [2,4]
 */
@PostMapping("/PostTransmit")
@ResponseBody
public List<Integer> PostTransmit(@RequestBody MapVo mapVo) {
	List<Integer> varList = new ArrayList<>();
	if (null != mapVo.getList()) {
		mapVo.getList().forEach(key -> varList.add(key));
		return varList;
	}
	return varList;
}

2.1 Post入参封装对象

// 对象入参
class MapVo {
	private List<Integer> list;

	public List<Integer> getList() {
		return list;
	}

	public void setList(List<Integer> list) {
		this.list = list;
	}
}
原文地址:https://www.cnblogs.com/Twittery/p/14504265.html