spring4.3.7 获取ajax传递的list等复杂类型的参数呢
spring4.3.7如何接收ajax传递的list等复杂类型呢?
直接传就被报
org.springframework.beans.InvalidPropertyException: Invalid property
'uAnswers0' of bean class [cn.xxx.entity.UAnswerList]:
Property referenced in indexed property path 'uAnswers0' is
neither an array nor a List nor a Map; returned value was
[cn.xxx.entity.UAnswer@2d3e47ed]
搜了下网上的解决方案,基本都是以前的,3.x的居多:
1.添加 jackson jar包 :
jackson-core
jackson-mapper
jackson-databind
2.配置xml
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list >
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</list>
</property>
</bean>
一个是AnnotationMethodHandlerAdapter属于被废弃的了,再一个就是依旧报错,被告知
org.springframework.web.util.NestedServletException: Request
processing failed; nested exception is
org.springframework.beans.InvalidPropertyException: Invalid property
'uAnswers0' of bean class [cn.xxx.entity.UAnswerList]:
Property referenced in indexed property path 'uAnswers0' is
neither an array nor a List nor a Map; returned value was
[cn.xxx.entity.UAnswer@70671304]
详细代码如下:
1、UAnswer类
public class UAnswer {
private Integer uaid;
private Integer quid;//用户id
private Integer qnid;//问卷id
private Integer qid;//问题id
private Integer oid;//选项id
private String remark;//备注
private String createtime;
public Integer getUaid() {
return uaid;
}
public void setUaid(Integer uaid) {
this.uaid = uaid;
}
public Integer getQuid() {
return quid;
}
public void setQuid(Integer quid) {
this.quid = quid;
}
public Integer getQnid() {
return qnid;
}
public void setQnid(Integer qnid) {
this.qnid = qnid;
}
public Integer getQid() {
return qid;
}
public void setQid(Integer qid) {
this.qid = qid;
}
public Integer getOid() {
return oid;
}
public void setOid(Integer oid) {
this.oid = oid;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
}
2、UAnswerList类
public class UAnswerList {
private Integer quid;//用户id
private Integer qnid;//问卷id
private List<UAnswer> uAnswers;
public Integer getQuid() {
return quid;
}
public void setQuid(Integer quid) {
this.quid = quid;
}
public Integer getQnid() {
return qnid;
}
public void setQnid(Integer qnid) {
this.qnid = qnid;
}
public List<UAnswer> getuAnswers() {
return uAnswers;
}
public void setuAnswers(List<UAnswer> uAnswers) {
this.uAnswers = uAnswers;
}
}
3、controller
@ResponseBody
@RequestMapping("addAnswer.do")
public String addAnswer(HttpServletRequest request, HttpServletResponse response, UAnswerList uAnswers){
ReturnResult r = new ReturnResult();
r.setCode(1);
r.setMsg("findAllNowCity success");
if(uAnswers==null||uAnswers.getQnid()==null||uAnswers.getQuid()==null||uAnswers.getuAnswers()==null){
r.setCode(-1);
r.setMsg("尚未填写问卷");
return r.toJsonString();
}
//相关操作
return r.toJsonString();
}
4、ajax模拟的数据
function yaya(){
$.ajax({
url: "http://192.168.0.251:8080/jsapi/addAnswer.do",
data: {
quid:"odeq5uFtR835lZGTKxLpGy9jHEDU",
qnid:1,
uAnswers:[
{
qid:1,
oid:2
},
{
qid:1,
oid:3
},
{
qid:2,
oid:7
},
{
qid:3,
oid:14
},
{
qid:6,
oid:15,
remark:"hahaha"
},
]
},
type: "post",
cache : false,
dataType:'json',
success: function(data) {
console.log(data);
document.getElementById("resurl").innerHTML= JSON.stringify(data);
},
});
}
也可能是ajax这块数据拼接格式的问题,但暂时不知该如何解决了。。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
用
@RequestBody UAnswerList uAnswers
2个地方需要修改:
使用@RequestBody注解,表示使用http body的内容,如下:
js端要使用json传送数据,所以需要指定application/json类型并使用JSON.stringify来将对象转成json格式,如下: