将json对象的数组发布到Spring MVC Controller

时间:2014-08-28 10:04:50

标签: jquery ajax json spring

我在发送和接收json对象列表(jQuery Ajax)时遇到问题

Java对象

public class UserSkill () {

 private long user;
 private long skill;
 private long level;

 //getter and setter methods

}

从Controller获取对象列表,它看起来像这样

$.getJSON("getList", function(data) {
    console.log(data);
});

//console log ->  [Object { user=4, skill=61, leve=10}, Object { user=3, skill=61, level=20}]

我更改了一些值,我们有以下代码

ioarray = [];

//update methods

console.log(ioarray);

//console log ->  [Object { user=4, skill=61, level=9000 }, Object { user=3, skill=61, level=100 }]

Ajax POST

$.ajax({
    url : 'goUpdate',
    type : 'POST',
    contentType : 'application/json; charset=utf-8',
    dataType : 'json',
    data: ioarray,
    succcess : function(e) {
        alert('Yeah');
}

控制器

@RequestMapping(value = "goUpdate", method = RequestMethod.POST)
    public Object goUpdatePOST(@RequestBody List<UserSkill> list) {

        //list.get(0).getLevel(); 

        return list;
}

日志

type Status report

message Request method 'POST' not supported

description The specified HTTP method is not allowed for the requested resource.

这里有什么问题......有什么想法吗?

UPDATE;

的Controler

@RequestMapping(value = "goUpdate", method = RequestMethod.POST)
public @ResponseBody String goUpdatePOST(@RequestBody UserSkill[] list) {

    for(UserSkill i : list) {
        System.out.println(i.getSkill());
    }
    return "somepage";

} 

的jQuery

var ioarray = [{ user:4, skill:61, level:10}, 
{ user:3,  skill:61, level:20}];

        $.ajax({
            url : 'goUpdate',
            type : 'POST',
            data: JSON.stringify(ioarray),
        });

控制台输出

JSON
0
    Object { user=4, skill=61, level=10}

1
    Object { user=3, skill=61, level=20}

Source
[{"user":4,"skill":61,"level":10},{"user":3,"skill":61,"level":20}]

插入pom.xml插入jackson-mapper-asl和jackson-core-asl。

当然这个例子会产生同样的错误......我做错了什么?我想我检查了所有可能性。

4 个答案:

答案 0 :(得分:2)

这对你有用吗<​​/ p>

    $.ajax({
        url : 'goUpdate',
        type : 'POST',
        data: JSON.stringify(ioarray),
        dataType: "json",
        contentType: "application/json"
    });

答案 1 :(得分:0)

尝试使用值=&#34; / goUpdate&#34;

@RequestMapping(value = "/goUpdate", method = RequestMethod.POST)

答案 2 :(得分:0)

我遇到同样的问题我最后没找到任何解决方案我找到了另一个使用简单HttpServletRequest的解决方案 您可以使用HttpServletRequest读取请求数据并使用JSON API将此json String转换为List<UserSkill>对象。您可以使用GSON API将json字符串转换为列表。

为此,您需要在控制器类方法中进行以下更改,如下所示:

@RequestMapping(value = "goUpdate", method = RequestMethod.POST)
public @ResponseBody String goUpdatePOST(HttpServletRequest request) 
{
    String jsonStr = IOUtils.toString(request.getInputStream());
    Gson gson = new Gson();
    Type  fooType = new TypeToken<List<UserSkill>>() {}.getType();
    List<UserSkill> list = gson.fromJson(jsonStr,fooType);
    return gson.toJson(list);
}  

作为响应发送JSON字符串,可以通过使用Gson API将List<UserSkill>转换为JSON字符串来创建。

愿这对你有所帮助。

答案 3 :(得分:0)

JS:

 $.ajax({
        type : "post",
        dataType : 'json', 
        url : 'addAdmin',  
        data : JSON.stringify(ioarray)
 })

Controller(注意它只需要一个自定义字符串)。然后它使用 Jackson 手动解析字符串,因为在这种情况下 Spring 不会这样做。 (如果您使用 @ModelAttribute 表单绑定,Spring 只会自动解析。)

@PostMapping("/addAdmin")
public boolean addAdmin(@RequestBody String json) throws Exception {

      String decodedJson = java.net.URLDecoder.decode(json, "UTF-8");
      ObjectMapper jacksonObjectMapper = new ObjectMapper(); // This is Jackson
      List<UserRolesGUIBean> userRolesGUIBeans =  jacksonObjectMapper.readValue(
              decodedJson, new TypeReference<List<UserRolesGUIBean>>(){});
      // Now I have my list of beans populated.
}
相关问题