2

I have the following Person class containing another Car class

public class Person {
    private String name
    private Car car;        
    ... getter and setters
}

public class Car {
    private String manufacturer;
    ... getter and setters
}

In Javascript I do the following:

person = new Object();
person.name = "Bob";

person.car = new Object();

car = person.car;

car.manufacturer = "Mercedes";

$.ajax({
    url: 'someurl.com',
    type: 'POST',
    data: person,
    dataType: 'json',
    ...
    complete: function() {
        ...
    }
});

When using the autobinding function I get the following error:

Function:

public @ResponseBody Car validateCar( Car car, HttpServletRequest servletRequest){
        ...
}

Error:

org.springframework.beans.InvalidPropertyException: Invalid property 'car[manufacturer]' of bean class [com.example.Person]: Property referenced in indexed property path 'car[manufacturer]' is neither an array nor a List nor a Map; returned value was []

What do I have to do to make the mapping work?

btw: my POST data looks like this:

name: Bob
car[manufacturer]: Mercedes

stringified as JSON it looks the following (i tested it but I don't stringify it)

{"name":"Bob",
 "car": {
  "manufacturer":"Mercedes"
 }
}
ndrizza
  • 3,285
  • 6
  • 27
  • 41

2 Answers2

0

It seems your passing in a Car hash and trying to autobind it to a Person object. For this to work, your hash would need to be look like person[car[manufacture]]]. Try passing in your person object in the javascript and then pulling the car object out of the autobinded person object.

vikash dat
  • 1,494
  • 2
  • 19
  • 37
  • thanks for pointing it out. i just did this error in my example. i've updated it now. in my real code the problem is still happening. – ndrizza Aug 20 '12 at 15:33
0

I think you are missing @RequestBody on your Car:

public @ResponseBody Car validateCar( @RequestBody Car car, HttpServletRequest servletRequest)
Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
  • Before just had the person class without the car class nested. And it was working fine without @RequestBody. If I use it now i get 415: unsupported media type. – ndrizza Aug 20 '12 at 15:38
  • Okay, good, this is more because of the Accept header not being present. I am not sure how it was working before without the @RequestBody annotation! Can you confirm you have `` annotation in your application context. – Biju Kunjummen Aug 20 '12 at 15:49
  • You also may have to stringify the data before posting - http://stackoverflow.com/questions/191881/serializing-to-json-in-jquery – Biju Kunjummen Aug 20 '12 at 16:12
  • i am sorry, i've tried both. 'mvc-annotation-driven' messes up the whole project and stringify causes '415 unsupported media' type errors. thanks for your help. i think i need to do a larger refactoring in my project. – ndrizza Aug 21 '12 at 08:43