Jackson XML 解析器:反序列化后获取空数据
Jackson XML Parser : Getting Null data after deserialization
我正在尝试使用 Jackson 反序列化 xml 数据。
我创建了 Pojo 类,但是在尝试打印数据时进行映射后,我得到的是 Null 值。
详情如下 -
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类
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
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
import java.io.Serializable;
public interface XmlData extends Serializable {
}
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);
}
}
在我的输出中,我得到了行标记中所有字段的空值。
VTX_SHS(row=[], reporting_date=20210228, interface_type=buss, event_type=green_light,
任何人都可以建议我在这里遗漏了什么吗?
此致
您需要使用正确的注解:JacksonXmlElementWrapper
示例:
@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);
}
控制台:
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
- 静态内部 类 仅用于示例目的。
- 请使用 LocalDate,日期已过时
我正在尝试使用 Jackson 反序列化 xml 数据。 我创建了 Pojo 类,但是在尝试打印数据时进行映射后,我得到的是 Null 值。
详情如下 -
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类
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
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
import java.io.Serializable;
public interface XmlData extends Serializable {
}
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);
}
}
在我的输出中,我得到了行标记中所有字段的空值。
VTX_SHS(row=[], reporting_date=20210228, interface_type=buss, event_type=green_light,
任何人都可以建议我在这里遗漏了什么吗?
此致
您需要使用正确的注解:JacksonXmlElementWrapper
示例:
@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);
}
控制台:
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
- 静态内部 类 仅用于示例目的。
- 请使用 LocalDate,日期已过时