I am trying to deserialize XML to an object in Java using the Jackson XML library. I have a class structure that has been generated from a schema. Here is an example of the classes:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "BijlageType", propOrder = {
"mimeContentId",
"mimeTypeCode",
"bestandsnaam"
})
public class BijlageType {
@XmlElement(name = "MimeContentId", required = true)
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "token")
protected String mimeContentId;
@XmlElement(name = "MimeTypeCode", required = true)
protected IANAMimeMediaTypeCodeType mimeTypeCode;
@XmlElement(name = "Bestandsnaam", required = true)
protected String bestandsnaam;
// getters and setters ...
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "IANAMimeMediaTypeCodeType", namespace = "urn:un:unece:uncefact:data:standard:QualifiedDataType:11", propOrder = {
"value"
})
public class IANAMimeMediaTypeCodeType {
@XmlValue
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
protected String value;
@XmlAttribute(name = "listID")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "token")
protected String listID;
@XmlAttribute(name = "listAgencyID")
protected AgencyIdentificationCodeContentType listAgencyID;
@XmlAttribute(name = "listVersionID")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "token")
protected String listVersionID;
@XmlAttribute(name = "listURI")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "token")
protected String listURI;
// getters and setters ...
}
@XmlType(name = "AgencyIdentificationCodeContentType", namespace = "urn:un:unece:uncefact:codelist:standard:6:3055:D09B")
@XmlEnum
public enum AgencyIdentificationCodeContentType {
EBV,
IANA;
public String value() {
return name();
}
public static AgencyIdentificationCodeContentType fromValue(String v) {
return valueOf(v);
}
}
Here is an example of the XML content I am trying to deserialize:
<ns0:Bijlage>
<gsk:MimeContentId>6816d9b3-d5fe-469e-877c-79aaa81777ca</gsk:MimeContentId>
<gsk:MimeTypeCode listAgencyID="IANA">application/pdf</gsk:MimeTypeCode>
<gsk:Bestandsnaam>202331509570500551.pdf</gsk:Bestandsnaam>
</ns0:Bijlage>
When I deserialize the XML to the Java objects, the mimeTypeCode value is null even though the listAgencyID is correctly set to “IANA”.
This issue occurs for any element that has an attribute(s) (listAgencyID=”IANA” is an example of one of them).
Here is the code I am using for deserialization:
private AanvraagType xmlContent(String urlPath) {
String url = fileService.getTextfileFromUrl(urlPath);
XmlMapper xmlMapper = getXmlWrapper();
AanvraagType data = xmlMapper.readValue(url, AanvraagType.class);
return data;
}
public static XmlMapper getXmlWrapper() {
// Create a JacksonXmlModule with default use of wrapper elements turned off
JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(false);
// Create an XmlMapper instance with the configured module
XmlMapper xmlMapper = new XmlMapper(module);
// Configure various settings for the XmlMapper instance
xmlMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
xmlMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
xmlMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
xmlMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
xmlMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
xmlMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
return xmlMapper;
}
Questions:
Why is the mimeTypeCode value not being set correctly during deserialization?
What is wrong with my setup or code that is causing this issue?
How can I solve this problem to ensure that the mimeTypeCode value and other objects are set up correctly deserialized?