私の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;
}
}
そして、ここでは、POJOにXMLを解析するためのコードです:
: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 [ソース: D:\ workspace \ test \ src \ test \ resources \ employee.xml; (参照チェーンを介し:request.Employees [ "従業員"]) 12:ライン:5、カラム com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:277)で com.fasterxmlで 。 jackson.databind.deser.SettableBeanProperty._throwAsIOE(SettableBeanProperty.java:551) でcom.fasterxml.jackson.databind.deser.SettableBeanProperty._throwAsIOE(SettableBeanProperty.java:532) com.fasterxml.jackson.databindで。 com.fasterxml.jackson.datでdeser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:108) でcom.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:276) com.fasterxml.jackson.databind.ObjectMapper.readValueでabind.deser.BeanDeserializer.deserialize com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3814)で(BeanDeserializer.java:140) ( sun.reflect.NativeMethodAccessorImplでrequest.Employees.setEmployees(Employees.java:31)で java.lang.NullPointerExceptionが:によって引き起こさtestPkg4.Test4.main(Test4.java:23)でObjectMapper.java:2756) で java.lang.reflect.Method.invoke(不明なソース)で sun.reflect.DelegatingMethodAccessorImpl.invoke(不明なソース)で sun.reflect.NativeMethodAccessorImpl.invoke(不明なソース)で.invoke0(ネイティブメソッド)com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:106) ... 5スレッド "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>
は、誰も私が問題だかを把握助けることができます!