如何将JSON数组传递给Spring-MVC? [英] How to pass a JSON array to Spring-MVC?

查看:67
本文介绍了如何将JSON数组传递给Spring-MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将JSON数组传递给Spring-MVC?

How to pass a JSON array to Spring-MVC?

我正在尝试寻找一种方法,以将JSON中的对象数组传递给Spring-MVC(Java).

I am trying to find a way to pass a array of objects in JSON to Spring-MVC(Java)..

下面是我的JavaScript,它设置了两个数组并进行了调用:

below is my javascript that setups the two arrays and makes the call:

function doAjaxPost() {  

      var inData = {};

      inData.name = ['kurt','johnathan'];
      inData.education = ['GSM','HardKnocks'];

      htmlStr = JSON.stringify(inData);
      alert(htmlStr);

      $.post( contexPath + "/AddUser.htm", inData, function(outData, outStatus){
          alert(outStatus);

      });
};

这是我的Java(Spring-MVC)控制器:

Here is my Java (Spring-MVC) Controller:

@RequestMapping(value="/AddUser.htm",method=RequestMethod.POST)
    public @ResponseBody JsonResponse addUser(@ModelAttribute(value="user") User user, BindingResult result ){
        JsonResponse res = new JsonResponse();
        ValidationUtils.rejectIfEmpty(result, "name", "Name can not be empty.");
        ValidationUtils.rejectIfEmpty(result, "education", "Educatioan not be empty");
        if(!result.hasErrors()){
            userList.add(user);
            res.setStatus("SUCCESS");
            res.setResult(userList);
        }else{
            res.setStatus("FAIL");
            res.setResult(result.getAllErrors());
        }

        return res;
    }

这是我正在使用的bean:

this is the bean I am using:

public class User {

    private String name = null;
    private String education = null;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEducation() {
        return education;
    }
    public void setEducation(String education) {
        this.education = education;
    }

}

请让我知道正确的操作方法...这是我得到的错误.严重:Servlet Spring MVC Dispatcher的Servlet.service()抛出异常org.springframework.beans.InvalidPropertyException:Bean类[com.raistudies.domain.User]的无效属性'education []':在索引属性路径'education []'中引用的属性既不是数组,也不是List也不是Map;返回值是[[Ljava.lang.String; @ 6fef3212]

please let me know the right what to get this working... this is the error I am getting.. SEVERE: Servlet.service() for servlet Spring MVC Dispatcher Servlet threw exception org.springframework.beans.InvalidPropertyException: Invalid property 'education[]' of bean class [com.raistudies.domain.User]: Property referenced in indexed property path 'education[]' is neither an array nor a List nor a Map; returned value was [[Ljava.lang.String;@6fef3212]

推荐答案

似乎JSON中的education属性是一个数组,但在POJO中它是一个字符串

Seems like the education property in your JSON is an array, but in your POJO it's a String

如果您更改

private String education = null

private List<String> education = null

它应该起作用,您还必须对名称做同样的事情.

It should work, you'll also have to do the same thing for the name.

这篇关于如何将JSON数组传递给Spring-MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆