2017-09-06 1180 views
0

这里是我的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> 

谁能帮我找出什么问题!

回答

0

找到一个有用的教程来创建自定义的非类型化的xml解串器,它帮助我克服了它。 以及序列化时使用jaxb2-maven-pluging作为构建插件从模式创建java类。

org.codehaus.mojo JAXB2-行家-插件 1。5 XJC -extension -npa -b $ {} project.basedir /src/main/xsd/global.xjb

**如果你正在使用杰克逊然后用jaxb注释的jackson替换替换注释。或者将jaxbannotation模块注册到您的序列化程序中。

这里是帮助我的gist的链接。