从xml到Java对象的Java jaxb解组返回空值

从xml到Java对象的Java jaxb解组返回空值,java,jaxb,unmarshalling,Java,Jaxb,Unmarshalling,您好,我是Jaxb的新手,已经花了3个多小时的时间搜索这个空点异常,但没有找到适合我的东西。我尝试使用inputStream、StringReader和Buffered reader,但当我尝试将XML文件解组到java对象时,所有结果都是空值,下面是一个例外: java.lang.IllegalStateException:未能执行CommandLineRunner 在org.springframework.boot.SpringApplication.callRunner(SpringApp

您好,我是Jaxb的新手,已经花了3个多小时的时间搜索这个空点异常,但没有找到适合我的东西。我尝试使用inputStream、StringReader和Buffered reader,但当我尝试将XML文件解组到java对象时,所有结果都是空值,下面是一个例外:

java.lang.IllegalStateException:未能执行CommandLineRunner 在org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:784)~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE] 在org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:765)~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE] 在org.springframework.boot.SpringApplication.run(SpringApplication.java:319)~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE] 在org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE] 在org.springframework.boot.SpringApplication.run(SpringApplication.java:1204)~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE] 在kso.xmlprocessing.XmlprocessingApplication.main(XmlprocessingApplication.java:10)~[classes/:na] 在java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机方法)~[na:na] 在java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)~[na:na] 在java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)~[na:na] 在java.base/java.lang.reflect.Method.invoke(Method.java:566)~[na:na] 在org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)~[spring-boot-devtools-2.1.8.RELEASE.jar:2.1.8.RELEASE] 原因:java.lang.NullPointerException:null 在kso.xmlprocessing.service.SupplierServiceImpl.seedSuppliers(SupplierServiceImpl.java:38)~[classes/:na] 在kso.xmlprocessing.web.controller.CarDealerController.run(CarDealerController.java:17)~[classes/:na] 在org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:781)~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE] ... 省略10个公共框架

我有这些课程:

@XmlRootElement(name = "supplier")
@XmlAccessorType(XmlAccessType.FIELD)
public class SupplierDto {
@XmlAttribute
private String name;
@XmlAttribute(name = "is-importer")
private Boolean isImporter;

public SupplierDto() {
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Boolean getImporter() {
    return isImporter;
}

public void setImporter(Boolean importer) {
    isImporter = importer;
}
}

这是XML文件:

<?xml version="1.0" encoding="utf-8"?>
 <suppliers>
 <supplier name="3M Company" is-importer="true"/>
 <supplier name="Agway Inc." is-importer="false"/>
 <supplier name="Anthem, Inc." is-importer="true"/>
 <supplier name="Airgas, Inc." is-importer="false"/>
 <supplier name="Big Lots, Inc." is-importer="true"/>
 <supplier name="Caterpillar Inc." is-importer="false"/>
 <supplier name="Casey's General Stores Inc." is-importer="true"/>
 <supplier name="Cintas Corp." is-importer="false"/>
 <supplier name="Chubb Corp" is-importer="true"/>
 <supplier name="Cintas Corp." is-importer="false"/>
 <supplier name="CNF Inc." is-importer="true"/>
 <supplier name="CMGI Inc." is-importer="true"/>
 <supplier name="The Clorox Co." is-importer="false"/>
 <supplier name="Danaher Corporation" is-importer="true"/>
 <supplier name="E.I. Du Pont de Nemours and Company" is- 
 importer="false"/>
 <supplier name="E*Trade Group, Inc." is-importer="true"/>
 <supplier name="Emcor Group Inc." is-importer="true"/>
 <supplier name="GenCorp Inc." is-importer="false"/>
 <supplier name="IDT Corporation" is-importer="true"/>
 <supplier name="Level 3 Communications Inc." is-importer="false"/>
 <supplier name="Merck &amp; Co., Inc." is-importer="true"/>
 <supplier name="Nicor Inc" is-importer="false"/>
 <supplier name="Olin Corp." is-importer="true"/>
 <supplier name="Paychex Inc" is-importer="true"/>
 <supplier name="Saks Inc" is-importer="false"/>
 <supplier name="Sunoco Inc." is-importer="true"/>
 <supplier name="Textron Inc" is-importer="true"/>
 <supplier name="VF Corporation" is-importer="false"/>
 <supplier name="Wyeth" is-importer="true"/>
 <supplier name="Zale" is-importer="false"/>
</suppliers>

问题在于POJO+注释没有正确映射到提供的xml。具体而言,在“SupplierRootDto”字段中,“supplierDtos”需要更改或使用注释进行覆盖,如下所示:

@XmlRootElement(name = "suppliers")
@XmlAccessorType(XmlAccessType.FIELD)
public class SupplierRootDto {

    // ADD NAME TO MATCH THE XML ELEMENT
    @XmlElement(name = "supplier")
    private List<SupplierDto> supplierDtos;

    public SupplierRootDto() {
    }

    public List<SupplierDto> getSupplierDtos() {
        return supplierDtos;
    }

    public void setSupplierDtos(List<SupplierDto> supplierDtos) {
        this.supplierDtos = supplierDtos;
    }
}
@XmlRootElement(name=“供应商”)
@XmlAccessorType(XmlAccessType.FIELD)
公共类供应商RootDTO{
//添加名称以匹配XML元素
@XmlElement(name=“供应商”)
私人名单供应商;
公共供应商root to(){
}
公共列表getSupplierDtos(){
退回供应商;
}
公共作废设置供应商操作系统(列出供应商操作系统){
this.supplierDtos=supplierDtos;
}
}
然后它将正确解组。

@XmlRootElement(name=“supplier”)
仅用于根节点。但是,在本例中,
不用作根节点。因此,您需要为其用法添加一个附加注释(有关更多详细信息,请参阅@mart的答案)
<?xml version="1.0" encoding="utf-8"?>
 <suppliers>
 <supplier name="3M Company" is-importer="true"/>
 <supplier name="Agway Inc." is-importer="false"/>
 <supplier name="Anthem, Inc." is-importer="true"/>
 <supplier name="Airgas, Inc." is-importer="false"/>
 <supplier name="Big Lots, Inc." is-importer="true"/>
 <supplier name="Caterpillar Inc." is-importer="false"/>
 <supplier name="Casey's General Stores Inc." is-importer="true"/>
 <supplier name="Cintas Corp." is-importer="false"/>
 <supplier name="Chubb Corp" is-importer="true"/>
 <supplier name="Cintas Corp." is-importer="false"/>
 <supplier name="CNF Inc." is-importer="true"/>
 <supplier name="CMGI Inc." is-importer="true"/>
 <supplier name="The Clorox Co." is-importer="false"/>
 <supplier name="Danaher Corporation" is-importer="true"/>
 <supplier name="E.I. Du Pont de Nemours and Company" is- 
 importer="false"/>
 <supplier name="E*Trade Group, Inc." is-importer="true"/>
 <supplier name="Emcor Group Inc." is-importer="true"/>
 <supplier name="GenCorp Inc." is-importer="false"/>
 <supplier name="IDT Corporation" is-importer="true"/>
 <supplier name="Level 3 Communications Inc." is-importer="false"/>
 <supplier name="Merck &amp; Co., Inc." is-importer="true"/>
 <supplier name="Nicor Inc" is-importer="false"/>
 <supplier name="Olin Corp." is-importer="true"/>
 <supplier name="Paychex Inc" is-importer="true"/>
 <supplier name="Saks Inc" is-importer="false"/>
 <supplier name="Sunoco Inc." is-importer="true"/>
 <supplier name="Textron Inc" is-importer="true"/>
 <supplier name="VF Corporation" is-importer="false"/>
 <supplier name="Wyeth" is-importer="true"/>
 <supplier name="Zale" is-importer="false"/>
</suppliers>
@XmlRootElement(name = "suppliers")
@XmlAccessorType(XmlAccessType.FIELD)
public class SupplierRootDto {

    // ADD NAME TO MATCH THE XML ELEMENT
    @XmlElement(name = "supplier")
    private List<SupplierDto> supplierDtos;

    public SupplierRootDto() {
    }

    public List<SupplierDto> getSupplierDtos() {
        return supplierDtos;
    }

    public void setSupplierDtos(List<SupplierDto> supplierDtos) {
        this.supplierDtos = supplierDtos;
    }
}