我正在使用jQuery将Ajax
Post请求发送到Spring MVC。其中两个值为String
数据类型,而其他两个值为int
。我收到以下错误:Can not instantiate variable of type.....From Integral number...
我正在接收从客户端发送到服务器端Java代码的数据,但是对象映射器的值为空(即personid = om.readValue(id, MyClass.class);
)并且这些相同的行是错误指向的位置。将Map<String,String>
更改为Map<String, Object>
也不会有效。
以下是我的代码。任何帮助将不胜感激。
JSON:
{
id:"35",
firstname : "John",
lastname : "Doe",
age: "40"
}
控制器:
@RequestMapping(value="/mywebservice", method = RequestMethod.POST)
public Object getPerson(@RequestBody Map<String, String> mydata){
MyClass personid = null;
MyClass fname = null;
MyClass lname = null;
MyClass personage = null;
String id = "";
String firstname = "";
String lastname = "";
String age = "";
ObjectMapper om = new ObjectMapper();
if(mydata.containsKey("id"){
id = mydata.get("id");
}
if(mydata.containsKey("firstname"){
firstname = mydata.get("firstname");
}
if(mydata.containsKey("lastname"){
lastname = mydata.get("lastname");
}
if(mydata.containsKey("age"){
age = mydata.get("age");
}
try{
personid = om.readValue(id, MyClass.class);
fname = om.readValue(firstname, MyClass.class);
lname = om.readValue(lastname, MyClass.class);
personage = om.readValue(age, MyClass.class);
}catch(Exception e){ ...}
return helperClass(personid, fname, lname, personage);
}
助手类:
public Object getFirstName(MyClass personid, MyClass fname, MyClass lname, MyClass personage){
return addName(personid, fname, lname, personage);
}
型号:
@JsonProperty("id")
private int childid;
@JsonProperty("firstname")
private String firstname;
@JsonProperty("lastname")
private String lastname;
@JsonProperty("age")
private int childage;
答案 0 :(得分:0)
尝试更改json?
{
id:35,
firstname : "John",
lastname : "Doe",
age: 40
}