spring4.3.7 How to receive complex types such as list passed by ajax?
It will be reported if it is passed directly
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]
After searching for solutions on the Internet, they are basically from the past, mostly 3.x ones:
1. Add jackson jar package:
jackson-core
jackson-mapper
jackson-databind
2.Configuration 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>
One is that AnnotationMethodHandlerAdapter is abandoned, and the other is that it still reports an error and is told
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 ]
The detailed code is as follows:
1. UAnswer class
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 class
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 simulated data
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);
},
});
}
It may also be a problem with the ajax data splicing format, but I don’t know how to solve it for the time being. .
Use
@RequestBody UAnswerList uAnswers
2 places need to be modified:
Use the @RequestBody annotation to indicate the use of http body content, as follows:
The js side needs to use json to transmit data, so you need to specify the application/json type and use JSON.stringify to convert the object into json format, as follows: