对象映射器值返回 Null
Object Mapper Values Returning Null
我正在使用 Ajax
Post 请求,使用 jQuery 将数据发送到 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);
}
帮手Class:
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;
尝试更改 json?
{
id:35,
firstname : "John",
lastname : "Doe",
age: 40
}
我正在使用 Ajax
Post 请求,使用 jQuery 将数据发送到 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);
}
帮手Class:
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;
尝试更改 json?
{
id:35,
firstname : "John",
lastname : "Doe",
age: 40
}