我可能会错过一些东西,因为我还没有完全理解一些基础知识。 在我的控制器中,我有类似于
的东西functiona(Person person){
System.out.println(p.firstName);
}
该视图具有以下内容
$.post("/validatePerson",{person: [{name:"first",value:"last"}]},function(result){
alert('done');
}
看起来信息正在传递但成员未填充。 我也尝试添加以下打字机
@Global
public class PersonJsonObjectBinder implements TypeBinder<Person> {
@Override
public Person bind(String name,
Annotation[] annotations,
String value, Class actualClass,
Type genericType) throws Exception
{
return new Gson().fromJson(value, Person.class);
}
//return new JsonParser().parse(value) ;}
}
你的帮助很大。
答案 0 :(得分:0)
如果要直接绑定到controller参数,则需要将Person数据作为请求uri的一部分传递。
$.post("/validatePerson?person.firstname=john&person.lastname=doe",function(result){
alert('done');
}
查看参数person.firstname如何匹配参数名称person和属性。
为了从Json绑定,您可以将主体作为json元素并使用GSON构建器进行反序列化。类似于以下内容
JsonElement jsonElement = new JsonParser().parse(body);
Person person = new GsonBuilder().create().fromJson(jsonElement,Person.class)
同样,json propety需要匹配Person类的属性,例如
{person: [{firstname:"john",lastname:"doe"}]}