1. 程式人生 > >springboot或springmvc傳遞複雜引數

springboot或springmvc傳遞複雜引數

本文使用的框架為springboot-2.0.4.RELEASE版本

1、傳遞List<實體類>

@PostMapping("/test")
public DataWrapper test(@RequestBody List<Jwt> jwts){
   return success(jwts);
}

這裡使用的註解為@RequestBody、使用該註解、前臺傳送請求時的Content-Type應該為:application/json型別、否則可能會接受不到引數

let jwt = [];//宣告一個數組
jwt.push({scope:'1111'});//加入陣列中
jwt.push({scope:'1111'});//加入陣列中
jwt.push({scope:'1111'});//加入陣列中
$.ajax({
	url:'xxxxx',
	Content-Type:'application/json;charset=UTF-8',
	data:jwt,//jwt 作為引數傳遞、也就是個List<jwt>型別
	//其他的省略
});

2、傳遞list<常規型別>如String、int等型別

@PostMapping("/test")
public DataWrapper test(@RequestParam List<Integer> ids,@RequestParam type){
    return success(ids);
}

這裡使用@RequestParam註解、Content-Type為application/x-www-form-urlencoded即可、至於其他的我沒試、以下是js程式碼的一個小片段

let ids = new Array();//這是陣列
for (var i = 0; i < length; i++) {
   ids.push(datas[i].id);
}
//這個是請求引數、請求為json型別、其中ids為陣列型別、轉換到後臺也就是List<int>或者int[]
//剩下的傳送請求等等省略      
let data = {
  'ids':ids,
  'type':type
}