spring4.3.7 获取ajax传递的list等复杂类型的参数呢

发布于 2022-09-04 22:23:26 字数 5403 浏览 34 评论 0

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

单身情人 2022-09-11 22:23:26

@RequestBody UAnswerList uAnswers

毁梦 2022-09-11 22:23:26
    @ResponseBody
    @RequestMapping("addAnswer.do")
    public String addAnswer(HttpServletRequest request, HttpServletResponse response, @RequestBody 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();
    }
莳間冲淡了誓言ζ 2022-09-11 22:23:26

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);
        
                    },
            });
      }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文