MVC控制器中反序列化json数组

MVC控制器中反序列化json数组

本文介绍了在Spring MVC控制器中反序列化json数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在发送json对象的列表,并尝试在Spring控制器中反序列化它.但是,一直以来,我都收到错误请求"的错误,并导致状态代码为415.但是,我的json数组是有效数组.

json是-

Ajax调用如下-

$.ajax({
url: $("#addNewUser").attr("action"),
data: JSON.stringify({users : dataToSend}),
dataType: 'json',
type: "POST",
beforeSend: function(xhr) {
    xhr.setRequestHeader("Accept", "application/json");
    xhr.setRequestHeader("Content-Type", "application/json");
},
success: function(data){
       alert('success=  ' + data);
},
error:function(data,status,er) {
    alert("error: "+ data.responseText +" status: "+status+" er:"+er);
}

});

包装器类如下. User和UserWrapper类-

public class User {

private String email;

private String userName;

private String user_id;

//getters and setters

}

public class UserWrapper {

private List<User> userList;

//getter and setter

}

最后是spring MVC控制器-

@RequestMapping(value="/user/add", method=RequestMethod.POST,
        produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public void createTeamMember(@RequestBody UserWrapper userWrapper) {

    try{
        for(User user : userWrapper.getUserList()){
            System.out.println(user.getEmail());
            }
    }catch(Exception ex){
        ex.printStackTrace();
    }

}

我在pom.xml中添加了对jackson-core和jackson-mapper的依赖.我正在使用Spring 4.0.3.感谢您的帮助.

解决方案

就像@shazin所说的那样,您很可能在您的问题中张贴了错误的方法,或者,如果没有,只需做出他建议的更改即可.

您将需要另一个解决方法,那就是将usersList属性从UserWrapper重命名为users,以使其与发送的JSON属性匹配.

我们提供了这些修复程序,您的请求没有问题,您应该不会再遇到问题了.

I am sending a list of json object and trying to deserialize it in my Spring controller. But all the time I am getting error of 'Bad request' and results into a status code of 415. However, my json array is a valid one.

json is -

Ajax call is as follows -

$.ajax({
url: $("#addNewUser").attr("action"),
data: JSON.stringify({users : dataToSend}),
dataType: 'json',
type: "POST",
beforeSend: function(xhr) {
    xhr.setRequestHeader("Accept", "application/json");
    xhr.setRequestHeader("Content-Type", "application/json");
},
success: function(data){
       alert('success=  ' + data);
},
error:function(data,status,er) {
    alert("error: "+ data.responseText +" status: "+status+" er:"+er);
}

});

Wrapper classes are as follows. User and UserWrapper class -

public class User {

private String email;

private String userName;

private String user_id;

//getters and setters

}

public class UserWrapper {

private List<User> userList;

//getter and setter

}

And finally the spring MVC controller is -

@RequestMapping(value="/user/add", method=RequestMethod.POST,
        produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public void createTeamMember(@RequestBody UserWrapper userWrapper) {

    try{
        for(User user : userWrapper.getUserList()){
            System.out.println(user.getEmail());
            }
    }catch(Exception ex){
        ex.printStackTrace();
    }

}

I've added dependency for jackson-core and jackson-mapper in pom.xml. And I am using Spring 4.0.3. Any help is appreciated.

解决方案

As @shazin is saying, you've most likely posted a wrong method to your question, or if not, simply make a change that he suggested.

You'll need another fix, and that is renaming the usersList property from UserWrapper to users so that it matches the JSON property being sent.

We these fixes, you should not have further problems, since your request is OK.

这篇关于在Spring MVC控制器中反序列化json数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 14:48