为了账号安全,请及时绑定邮箱和手机立即绑定

前台传json对象数组,java后台用List接受时报错

前台传json对象数组,java后台用List接受时报错

侃侃尔雅 2019-02-17 00:58:20
spring 项目中的controller是不是不能直接接受对象类型的List为入参?为什么?找了很多相关文档,都说不行,要不就传一个vo类,vo类里面有list,要不就传字符串,后台解析字符串 spring boot 项目: public class Tag { int id; String name; 此处省略了get,set } @RestController public class HelloController { @RequestMapping(value = "add", consumes = "application/json; charset=utf-8") public String add(@RequestBody List<Tag> param) { System.out.println("param:" + param); return "成功"; } } commit(){ $.ajax({ type:"post", url: 'add', contentType:"application/json; charset=utf-8", dataType:"json", data:{ param:JSON.stringify([{id:1,name:'hello'},{id:2,name:'hello'}]) }, success:function(data){ console.log(data); } }) } 后台报错信息: 2018-01-29 13:52:57.856 WARN 3148 --- [nio-8088-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized token 'param': was expecting ('true', 'false' or 'null'); nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'param': was expecting ('true', 'false' or 'null')
查看完整描述

1 回答

?
偶然的你

TA贡献1841条经验 获得超3个赞

data:JSON.stringify([{id:1,name:'hello'},{id:2,name:'hello'}])

查看完整回答
反对 回复 2019-03-01
  • 1 回答
  • 0 关注
  • 1745 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号