gpt4 book ai didi

Java jaxb从xml解码到java对象返回空值

转载 作者:行者123 更新时间:2023-12-02 09:31:05 24 4
gpt4 key购买 nike

你好,我是 Jaxb 的新手,已经花了 3 个多小时用这个解码的东西搜索这个 NullPointerException ,但没有发现任何对我有用的东西。我尝试使用 inputStream、StringReader 和 Buffered reader,但是当我尝试将 XML 文件解码为 java 对象时,所有结果都为空值,这是一个异常(exception):

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( native 方法) ~[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个常见框架

我有这些类(class):

@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;
}
}

然后它将正确解码。

关于Java jaxb从xml解码到java对象返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57966539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com