2

我有以下Person包含另一个类的Car

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

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

在 Javascript 中,我执行以下操作:

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() {
        ...
    }
});

使用自动绑定功能时,出现以下错误:

功能:

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

错误:

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 []

我必须做什么才能使映射工作?

顺便说一句:我的 POST 数据如下所示:

name: Bob
car[manufacturer]: Mercedes

字符串化为 JSON 它看起来如下(我对其进行了测试,但我没有对其进行字符串化)

{"name":"Bob",
 "car": {
  "manufacturer":"Mercedes"
 }
}
4

2 回答 2

0

似乎您传入了 Car 哈希并试图将其自动绑定到 Person 对象。为此,您的哈希需要看起来像 person[car[manufacture]]]。尝试在 javascript 中传入您的人员对象,然后将汽车对象从自动绑定的人员对象中拉出。

于 2012-08-20T15:26:05.863 回答
0

我认为您的汽车上缺少@RequestBody:

public @ResponseBody Car validateCar( @RequestBody Car car, HttpServletRequest servletRequest)
于 2012-08-20T15:34:09.120 回答