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;
	}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM