[英]Jackson XML Parser : Getting Null data after deserialization
I am trying deserialize the xml data using Jackson.我正在尝试使用 Jackson 反序列化 xml 数据。 I created the Pojo classes but after mapping when i try to print the data, I am getting Null value.我创建了 Pojo 类,但是在尝试打印数据时映射后,我得到了 Null 值。
Below are the details -以下是详细信息——
XML Data XML 数据
<?xml version="1.0" encoding="UTF-8"?>
<VTX_SHS reporting_date="20210228" interface_type="buss" event_type="green_light" >
<!-- 01: period | varchar2(10) -->
<!-- 02: book_base_ent_cd | varchar2(10) -->
<!-- 03: isin | varchar2(12) -->
<row period="2021-02-28" book_base_ent_cd="U0027" other_inst_ident="PLCHS258Q463" />
<row period="2021-02-28" book_base_ent_cd="U0028" other_inst_ident="PLCHS258Q464" />
<row period="2021-02-28" book_base_ent_cd="U0029" other_inst_ident="PLCHS258Q465" />
</VTX_SHS>
POJO Classes POJO 类
VTX_SHS.java VTX_SHS.java
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import java.util.List;
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class VTX_SHS implements XmlData{
public List<row> row;
public int reporting_date;
public String interface_type;
public String event_type;
}
row.java行.java
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import java.util.Date;
import java.util.List;
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class row implements XmlData {
public Date period;
public String book_base_ent_cd;
public String other_inst_ident;
}
XmlData.java XmlData.java
import java.io.Serializable;
public interface XmlData extends Serializable {
}
SHSCompare.java SHSCompare.java
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import lombok.var;
import models.VTX_SHS;
import java.io.File;
import java.io.IOException;
public class SHSCompare {
public static void main(String[] args) throws IOException {
XmlMapper xmlMapper = new XmlMapper();
File file = new File("src/test/resources/Files/data.xml");
var SHS1Response = xmlMapper.readValue(file, VTX_SHS.class);
System.out.println(SHS1Response);
}
}
In my output I am getting null values for all the fields in row tag.在我的 output 中,我得到了行标签中所有字段的 null 值。
VTX_SHS(row=[], reporting_date=20210228, interface_type=buss, event_type=green_light,
Can anyone suggest what have I missed here?谁能建议我在这里错过了什么?
Best Regards此致
You need to use the proper annotation: JacksonXmlElementWrapper
您需要使用正确的注释: JacksonXmlElementWrapper
http://fasterxml.github.io/jackson-dataformat-xml/javadoc/2.2.0/com/fasterxml/jackson/dataformat/xml/annotation/JacksonXmlElementWrapper.html http://fasterxml.github.io/jackson-dataformat-xml/javadoc/2.2.0/com/fasterxml/jackson/dataformat/xml/annotation/JacksonXmlElementWrapper.Z7FC365FDC3ADD2FC2C9D5398E
Example:例子:
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
static class VTX_SHS implements Serializable {
@JacksonXmlElementWrapper(useWrapping = false)
public List<Row> row;
public int reporting_date;
public String interface_type;
public String event_type;
}
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
static class Row implements Serializable {
public Date period;
public String book_base_ent_cd;
public String other_inst_ident;
}
public static void main(String[] args) throws JsonProcessingException {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<VTX_SHS reporting_date=\"20210228\" interface_type=\"buss\" event_type=\"green_light\" >\n"
+ " <!-- 01: period | varchar2(10) -->\n"
+ " <!-- 02: book_base_ent_cd | varchar2(10) -->\n"
+ " <!-- 03: isin | varchar2(12) -->\n"
+ " \n"
+ " <row period=\"2021-02-28\" book_base_ent_cd=\"U0027\" other_inst_ident=\"PLCHS258Q463\" />\n"
+ " <row period=\"2021-02-28\" book_base_ent_cd=\"U0028\" other_inst_ident=\"PLCHS258Q464\" />\n"
+ " <row period=\"2021-02-28\" book_base_ent_cd=\"U0029\" other_inst_ident=\"PLCHS258Q465\" />\n"
+ " \n"
+ "</VTX_SHS>";
XmlMapper xmlMapper = new XmlMapper();
var SHS1Response = xmlMapper.readValue(xml, VTX_SHS.class);
System.out.println(SHS1Response);
}
Console:安慰:
VTX_SHS(row=[Row(period=Sun Feb 28 01:00:00 CET 2021, book_base_ent_cd=U0027, other_inst_ident=PLCHS258Q463), Row(period=Sun Feb 28 01:00:00 CET 2021, book_base_ent_cd=U0028, other_inst_ident=PLCHS258Q464), Row(period=Sun Feb 28 01:00:00 CET 2021, book_base_ent_cd=U0029, other_inst_ident=PLCHS258Q465)], reporting_date=20210228, interface_type=buss, event_type=green_light)
Process finished with exit code 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.