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
[cn.xxx.entity.UAnswer@70671304 ]
processing failed; nested exception is
org.springframework.beans.InvalidPropertyException: Invalid property
'uAnserswork.beans.InvalidPropertyException: Invalid property
'uAnsswers0' of bean swid | .xxx.entity.UAnswerList]:
Property referenced in indexed property path 'uAnswers0' is
neither an array nor a List nor a Map; returned value was
詳細程式碼如下:
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這塊資料拼接格式的問題,但暫時不知該如何解決了。 。 ###漂亮男人2017-05-16 13:27:28
2個地方要修改:
使用@RequestBody註解,表示使用http body的內容,如下:
@ResponseBody
@RequestMapping("addAnswer.do")
public String addAnswer(HttpServletRequest request, HttpServletResponse response, @RequestBody UAnswerList uAnswers){ 省略 }
js端要使用json傳送數據,所以需要指定application/json類型並使用JSON.stringify來將物件轉換成json格式,如下:
function yaya(){
$.ajax({
url: "http://192.168.0.251:8080/jsapi/addAnswer.do",
contentType:"application/json", //新增
data: JSON.stringify({
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);
},
});
}