Java jaxb 从 xml 解组到 java 对象 returns 空值

Java jaxb unmarshalling from xml to java object returns null value

你好,我是 Jaxb 的新手,已经花了 3 个多小时搜索这个 NullPointerException 和这个解组的东西,但没有找到对我有用的东西。我尝试使用 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) ~[类/: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:空 在 kso.xmlprocessing.service.SupplierServiceImpl.seedSuppliers(SupplierServiceImpl.java:38) ~[类/:na] 在 kso.xmlprocessing.web.controller.CarDealerController.run(CarDealerController.java:17) ~[类/: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;
}

}

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

@XmlElement
private List<SupplierDto> supplierDtos;

public SupplierRootDto() {
}

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

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

}

这是我解析数据的逻辑:

JAXBContext context = JAXBContext.newInstance(SupplierRootDto.class);
    Unmarshaller unmarshaller = context.createUnmarshaller();

    String path = "suppliers.xml";
    BufferedReader reader = new BufferedReader(new FileReader(new 
File(path)));

    SupplierRootDto list = (SupplierRootDto) 
unmarshaller.unmarshal(reader);

    for (SupplierDto supplierDto : list.getSupplierDtos()) {

this.supplierRepository.saveAndFlush(this.modelMapper.map(supplierDto, 
Supplier.class));
    }

这是 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>

问题是您的 POJOS + 注释没有正确映射到提供的 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;
    }
}

然后它将正确解组。