javascript - spring4.3.7 获取ajax传递的list等复杂类型的参数呢
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-05-16 13:25:28
0
3
567

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这块数据拼接格式的问题,但暂时不知该如何解决了。。

曾经蜡笔没有小新
曾经蜡笔没有小新

全部回复(3)
黄舟

@RequestBody UAnswerList uAnswers

伊谢尔伦

雷雷

漂亮男人

2个地方需要修改:

  1. 使用@RequestBody注解,表示使用http body的内容,如下:

@ResponseBody
@RequestMapping("addAnswer.do")
public String addAnswer(HttpServletRequest request, HttpServletResponse response, @RequestBody UAnswerList uAnswers){ 省略 }
  1. 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);
        
                    },
            });
      }
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板