需求:因为需要同步某一年的所有用户数据,用户数量有1W+,每个用户字段有10+,数据太长,发送请求时,后台无法接收。
网上有方法要修改tomcat配置文件,但这个项目部署在nginx上,可通过修改前台发送的content-type改为json格式发送。正常使用x-www-form-unlencoded格式发送可以参考我另一篇文章。
https://www.cnblogs.com/mogujun/articles/12180459.html
两者在后台接收参数的时候有区别。
使用x-www-form-unlencoded发送时,后台只需要在接口输入参数名字与之对应即可。
使用json发送时,后台需要新建一个与之对应的实体类,用实体类来接收。

1 Axios({ 2 method: 'post', 3 url: '/xxxxx', 4 data:{ 5 xxx: xxx, 6 xxxx: xxxx, 7 }, 8 transformRequest: [ 9 function (data) { 10 let ret = '' 11 for (let it in data) { 12 ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&' 13 } 14 ret = ret.substring(0, ret.lastIndexOf('&')); 15 return ret 16 } 17 ], 18 headers: { 19 'Content-Type': 'application/x-www-form-urlencoded' 20 }, 21 }).then((res) => { 22 console.log(res); 23 }).catch((err) => { 24 console.log(err); 25 })

1 Axios({ 2 method: 'post', 3 url: '/xxxxx', 4 data:JSON.stringify(response.data), 5 headers: { 6 'Content-Type': 'application/json;charset=UTF-8' 7 }, 8 }).then((res) => { 9 console.log(res.data); 10 }).catch((err) => { 11 console.log(err); 12 });

1 @RequestMapping("/xxxxx") 2 @ResponseBody 3 public String syncStuData(@RequestBody syncDomain syncData) throws Exception{ 4 System.out.println(syncData); 5 hwAdminService.insertAcademic(syncData.getAcademicList()); 6 hwAdminService.insertClass(syncData.getClassList()); 7 hwAdminService.insertStudent(syncData.getStudentList()); 8 9 return "success"; 10 }