我收到来自 Web 服务的 XML 响应,但无法将其转换为 POJO。
容纳数据的类就是这个。
public class ApaData {
private String processCode;
private String processLabel;
private List<Step> steps;
}
这是一个简单的类,具有一些基本属性和一个名为step的嵌套类的列表。
public class Step {
private Integer StepNum;
private String StepStatus;
private String StepLabel;
}
传入的 XML 是这个:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<APA_Data xmlns="http://www.test.com/atlas/apa/v1">
<ProcessCode>4</ProcessCode>
<ProcessLabel>information is invalid.</ProcessLabel>
<Step>
<StepNum>1</StepNum>
<StepStatus>9</StepStatus>
<StepLabel>Decryption not attempted.</StepLabel>
</Step>
<Step>
<StepNum>2</StepNum>
<StepStatus>9</StepStatus>
<StepLabel>Digital signature verification not attempted.</StepLabel>
</Step>
</APA_Data>
但是转换失败。
@Test
void noSequence() {
String buffer = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
"<APA_Data xmlns=\"http://www.test.com/atlas/apa/v1\">\n" +
" <ProcessCode>4</ProcessCode>\n" +
" <ProcessLabel>Either reporting/observed agent or period information is invalid.</ProcessLabel>\n" +
" <Step>\n" +
" <StepNum>1</StepNum>\n" +
" <StepStatus>9</StepStatus>\n" +
" <StepLabel>Decryption not attempted.</StepLabel>\n" +
" </Step>\n" +
" <Step>\n" +
" <StepNum>2</StepNum>\n" +
" <StepStatus>9</StepStatus>\n" +
" <StepLabel>Digital signature verification not attempted.</StepLabel>\n" +
" </Step>\n" +
"</APA_Data>";
try {
ObjectMapper objectMapper;
JacksonXmlModule xmlModule = new JacksonXmlModule();
xmlModule.setDefaultUseWrapper(false);
objectMapper = new XmlMapper(xmlModule);
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
objectMapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);
objectMapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_VALUES);
objectMapper.enable(MapperFeature.IGNORE_MERGE_FOR_UNMERGEABLE);
final ApaData apaData = objectMapper.readValue(buffer, new TypeReference<ApaData>() {
});
log.info("Hello {}", apaData);
for(Step step : apaData.getSteps()) {
log.info("step {}", step);
}
} catch (Exception e) {
log.info("test failed {}", e.getMessage());
}
}
报告的问题是:
测试失败无法识别的字段“Step”(类 com.adapters.ccr.ApaData),未标记为可忽略(3 已知 属性:“processCode”、“processLabel”、“steps”])位于 [来源: (字符串读取器);行:6,列:18](通过参考链: com.adapters.ccr.ApaData["步骤"])
我希望在我的 XML 消息中收到一个名为 Steps 的父列表对象以便继续,但不幸的是 XML 中没有“序列”标签。
是否可以以某种方式解析此 XML 并创建 POJO?
POJO 中的
step
属性不应采用复数形式,即使它是无界属性。
从 xsd 生成时,由于 xjc 的做法,属性采用复数形式,但也有带有真实属性名称的注释。