I am receiving an XML response from a web service that I cannot converted to POJOs.
The class that will accommodate the data is this one.
public class ApaData {
private String processCode;
private String processLabel;
private List<Step> steps;
}
It is a simple class with some basic attributes and a List of a nested class called step.
public class Step {
private Integer StepNum;
private String StepStatus;
private String StepLabel;
}
The incoming XML is this one:
<?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>
but the conversion fails.
@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());
}
}
The problem reported is:
test failed Unrecognized field "Step" (class com.adapters.ccr.ApaData), not marked as ignorable (3 known properties: "processCode", "processLabel", "steps"]) at [Source: (StringReader); line: 6, column: 18] (through reference chain: com.adapters.ccr.ApaData["Step"])
I would expect to receive in my XML message a parent list object called Steps in order to proceed but unfortunately there is no "sequence" tag in the XML.
Is this possible to somehow parse this XML and create POJOs?
The step
property should be not in plural form in POJO even if it's a unbounded property.
When generating from xsd, the property is in plural form, due to xjc way of doing, but does also have an annotation with the real property name.
You can also add the @XmlElement(name = "step")
annotation on the steps
property to fix your problem