46

My code is as below:

controller

@RequestMapping(value="/setTest", method=RequestMethod.POST, consumes="application/json")
public @ResponseBody ModelMap setTest(@RequestBody List<TestS> refunds, ModelMap map) {
    for(TestS r : refunds) {
        System.out.println(r.getName());
    }
    // other codes
}

TestS pojo

public class TestS implements Serializable {
    private String name;
    private String age;
    //getter setter
}

Json request

var z = '[{"name":"1","age":"2"},{"name":"1","age":"3"}]';
$.ajax({
    url: "/setTest",
    data: z,
    type: "POST",
    dataType:"json",
    contentType:'application/json'               
});

It's giving this error

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.air.cidb.entities.TestS

I am using spring 3.1.2 and jackson 2.0.4

Edit: I want to receive list of TestS objects at my controller method, and process them. I am not able to find if I am sending wrong json or my method signature is wrong.

HKumar
  • 655
  • 1
  • 8
  • 18
  • Are you sure this is your current code? The client calls `/cmp/approveRefunds` and your controller is mapped to `/setTest`. – Dirk Lachowski Apr 11 '14 at 12:45
  • 1
    refer this,http://stackoverflow.com/questions/10864049/map-json-array-of-objects-to-requestbody-listt-using-jackson – Touchstone Apr 11 '14 at 12:58

6 Answers6

48

Here is the code that works for me. The key is that you need a wrapper class.

public class Person {

    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }

A PersonWrapper class

public class PersonWrapper {

    private List<Person> persons;

    /**
     * @return the persons
     */
    public List<Person> getPersons() {
        return persons;
    }

    /**
     * @param persons the persons to set
     */
    public void setPersons(List<Person> persons) {
        this.persons = persons;
    }
}

My Controller methods

@RequestMapping(value="person", method=RequestMethod.POST,consumes="application/json",produces="application/json")
    @ResponseBody
    public List<String> savePerson(@RequestBody PersonWrapper wrapper) {
        List<String> response = new ArrayList<String>();
        for (Person person: wrapper.getPersons()){
        personService.save(person);
         response.add("Saved person: " + person.toString());
    }
        return response;
    }

The request sent is json in POST

{"persons":[{"name":"shail1","age":"2"},{"name":"shail2","age":"3"}]}

And the response is

["Saved person: Person [name=shail1, age=2]","Saved person: Person [name=shail2, age=3]"]
Shailendra
  • 8,874
  • 2
  • 28
  • 37
  • 4
    Thanks for the answer. But I want to know why do we need to make a wrapper? Why can't this be done using a Map? – Ujwal Ratra Apr 21 '16 at 05:56
  • Here how can I name the persons tag differently --> {"CustomName":[{"name":"shail1","age":"2"},{"name":"shail2","age":"3"}]}? – Arpan Banerjee Jul 31 '20 at 14:38
14

This is not possible the way you are trying it. The Jackson unmarshalling works on the compiled java code after type erasure. So your

public @ResponseBody ModelMap setTest(@RequestBody List<TestS> refunds, ModelMap map) 

is really only

public @ResponseBody ModelMap setTest(@RequestBody List refunds, ModelMap map) 

(no generics in the list arg).

The default type Jackson creates when unmarshalling a List is a LinkedHashMap.

As mentioned by @Saint you can circumvent this by creating your own type for the list like so:

class TestSList extends ArrayList<TestS> { }

and then modifying your controller signature to

public @ResponseBody ModelMap setTest(@RequestBody TestSList refunds, ModelMap map) {
Dirk Lachowski
  • 3,121
  • 4
  • 40
  • 66
  • 2
    This is not true. Spring 4 supports this. You can always get the parameterized method parameter with their actual type arguments through reflection. – Sotirios Delimanolis Apr 11 '14 at 13:50
  • But since they aren't using Spring 4, that part of your answer is appropriate. Remove the type erasure bit. – Sotirios Delimanolis Apr 11 '14 at 13:51
  • @SotiriosDelimanolis Nice hint, didn't knowed that. Could you direct me to the part in the spring docs where it's mentioned or is it buried in the sources? – Dirk Lachowski Apr 11 '14 at 15:46
  • I don't have the links with me right now. For the reflection part, you can use `Method#getGenericParameterTypes()` to get the type information. As for Jackson working with that there must be an entry in the MVC chapter. – Sotirios Delimanolis Apr 11 '14 at 17:16
12
@RequestMapping(
         value="person", 
         method=RequestMethod.POST,
         consumes="application/json",
         produces="application/json")
@ResponseBody
public List<String> savePerson(@RequestBody Person[] personArray) {
    List<String> response = new ArrayList<String>();
    for (Person person: personArray) {
        personService.save(person);
        response.add("Saved person: " + person.toString());
    }
    return response;
}

We can use Array as shown above.

rohanagarwal
  • 771
  • 9
  • 30
Deepak
  • 1,670
  • 1
  • 20
  • 20
  • This should be the accepted answer. Take an array as the body and convert it to a list in the controller method. No need for wrapper classes or to change the format of your JSON. – Zack Oct 23 '20 at 18:56
3

Solution works very well,

public List<String> savePerson(@RequestBody Person[] personArray)  

For this signature you can pass Person array from postman like

[
{
  "empId": "10001",
  "tier": "Single",
  "someting": 6,
  "anything": 0,
  "frequency": "Quaterly"
}, {
  "empId": "10001",
  "tier": "Single",
  "someting": 6,
  "anything": 0,
  "frequency": "Quaterly"
}
]

Don't forget to add consumes tag:

@RequestMapping(value = "/getEmployeeList", method = RequestMethod.POST, consumes="application/json", produces = "application/json")
public List<Employee> getEmployeeDataList(@RequestBody Employee[] employeearray) { ... }
mcy
  • 1,209
  • 6
  • 25
  • 37
AmitG
  • 31
  • 1
1

I believe this will solve the issue

var z = '[{"name":"1","age":"2"},{"name":"1","age":"3"}]';
z = JSON.stringify(JSON.parse(z));
$.ajax({
    url: "/setTest",
    data: z,
    type: "POST",
    dataType:"json",
    contentType:'application/json'               
});
Jis Mathew
  • 175
  • 1
  • 7
0

For me below code worked, first sending json string with proper headers

$.ajax({
        type: "POST",
        url : 'save',
        data : JSON.stringify(valObject),
        contentType:"application/json; charset=utf-8",
        dataType:"json",
        success : function(resp){
            console.log(resp);
        },
        error : function(resp){
            console.log(resp);
        }
    });

And then on Spring side -

@RequestMapping(value = "/save", 
                method = RequestMethod.POST,
                consumes="application/json")
public @ResponseBody String  save(@RequestBody ArrayList<KeyValue> keyValList) {
    //Saving call goes here
    return "";
}

Here KeyValue is simple pojo that corresponds to your JSON structure also you can add produces as you wish, I am simply returning string.

My json object is like this -

[{"storedKey":"vc","storedValue":"1","clientId":"1","locationId":"1"},
 {"storedKey":"vr","storedValue":"","clientId":"1","locationId":"1"}]
Pratik Goenka
  • 2,321
  • 1
  • 27
  • 24