springboot接收前端的数组_SpringBoot接收前端参数的⼏种⽅
昨天和前端⼩伙伴在联调是碰到了参数接收不到的错误,我在postman上测试接⼝是正常的,但是与前端对接时就接受不到参数,请求⽅式都是get,但是问题就在于json  和 form-data 的区别!这是⼀个SpringBoot+Vue的前后端分离项⽬
1)如果前端传的是json,后端接受时要⽤@RequestBody注解,json对象要与实体类对应
public boolean updateKnowledge(@RequestBody Knowledge knowledge){
return knowledgeService.updateKnowledge(knowledge);
}
如果json没有与实体类对应的话,可以有另外⼀种⽅法,利⽤JSONObject对象取值,可以直接转换成你需要的类型,这⾥仅展⽰string类型
public int deleteKnowledge(@RequestBody JSONObject obj){
return knowledgeService.String("id"));
}
2)如果前端传的是form-data,后端接受时要⽤@RequestParam注解,表单的key也要与实体类的字段对应
public void downloadFile( @RequestParam("groupName") String groupName,@RequestParam("url") String url, HttpServletResponse response) throws IOException {
MyFile myFile = new MyFile();
myFile.setGroupName(groupName);
vuejson转对象myFile.setUrl(url

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。