這裏是我的bean類:如何解決這個JsonMappingException:N/A XML反序列化過程
package request;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
@JacksonXmlRootElement
public class Employee {
private List<String> roles= new ArrayList<String>();
private String name;
public Employee(){}
@JacksonXmlProperty
public String getName()
{
return name;
}
public void setName (String name)
{
this.name = name;
}
@JacksonXmlElementWrapper(useWrapping=false)
@JacksonXmlProperty
public List<String> getRoles()
{
return roleCodes;
}
public void setRoles (String role)
{
this.roles.add(role);
}
}
,並
package request;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
public class Employees
{
@JacksonXmlElementWrapper(localName="employees")
@JacksonXmlProperty(localName="employee")
private ArrayList<Employee> emps;
//Employee Employee ;
public Employees(){}
@JacksonXmlProperty(localName="employee")
public ArrayList<Employee> getEmployees()
{
return emps;
}
public void setEmployees(Employee emp){
this.emps.add(emp);
}
@Override
public String toString()
{
if(emps.isEmpty()!=true)
for (Employee e:emps)
return "this is [employee = "+e ;
return "none there";
}
public ArrayList<Employee> addingEmployee(Employee e){
this.emps.add(e);
return emps;
}
}
這裏是解析XML到POJO代碼:
package testPkg4;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import request.Bean;
import request.Employee;
import request.Employees;
public class Test4 {
public static void main(String[] args) {
XmlMapper xmlMapper = new XmlMapper();
//Bean value = new Bean();
Employees emps=new Employees();
try {
emps = xmlMapper.readValue(new File("D:\\workspace\\test\\src\\test\\resources\\employee.xml"),
Employees.class);
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
System.out.println(emps.getEmployees().get(0).getFirstName());
//System.out.println(e.getFirstName());
//System.out.println(emps.getEmployees().get(0).getThirdElement());
}
}
現在,這裏是我得到的錯誤:
com.fasterxml.jackson.databind.JsonMappingException:N/A at [Source: D:\ workspace \ test \ src \ test \ resources \ employee.xml;行:5,列: 12](通過引用鏈:request.Employees [「employee」]) com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:277) at com.fasterxml。 jackson.databind.deser.SettableBeanProperty._throwAsIOE(SettableBeanProperty.java:551) 在 com.fasterxml.jackson.databind.deser.SettableBeanProperty._throwAsIOE(SettableBeanProperty.java:532) 在 com.fasterxml.jackson.databind。 deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:108) 在 com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:276) 在 com.fasterxml.jackson.dat abind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:140) 在 com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3814) 在 com.fasterxml.jackson.databind.ObjectMapper.readValue(在 sun.reflect.NativeMethodAccessorImpl在 request.Employees.setEmployees(Employees.java:31) 顯示java.lang.NullPointerException:ObjectMapper.java:2756) 在testPkg4.Test4.main(Test4.java:23)引起的.invoke0(本機方法)維持在 sun.reflect.DelegatingMethodAccessorImpl.invoke(來源不明) sun.reflect.NativeMethodAccessorImpl.invoke(來源不明)處 java.lang.reflect.Method.invoke(來源不明)com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:106) ... 5 more線程「main」中的異常java.lang.NullPointerException at testPkg4.Test4.main(Test4。 Java的:29)
而我解析thsi XML文件:
<employees>
<employee>
<name>ASHISH</name>
<roles>MD</roles>
</employee>
<employee>
<name>BAHADUR</name>
<roles>CO</roles>
<roles>TM</roles>
</employee>
</employees>
誰能幫我找出什麼問題!