XML JAXB Serializer and Deserializer
Use JAXBSerializer and JAXBDeserializer to serialize and deserialize Java XML JAXB object to and from the Candid payload of type RECORD.
Java Architecture for XML Binding (JAXB) is a standard that defines an API for reading and writing Java objects to and from XML documents. It applies a lot of defaults, thus making reading and writing of XML via Java relatively easy.
With Java releases lower than Java 11, JAXB was part of the JVM and you could use it directly without defining additional libaries.
As of Java 11, JAXB is not part of the JRE anymore and you need to configure the relevant libraries via your dependency management system, for example Maven or Gradle.
A fully functional example using JAXBSerializer and JAXBDeserializer can be found here.
This example uses Motoko to call the canister main.mo. The canister uses 2 complex types, LoanApplication and LoanOffer.
// Loan Application
public type LoanApplication = {
id: Int;
firstname: Text;
lastname: Text;
zipcode: Text;
ssn: Text;
amount: Float;
term: Int16;
created: Int;
};
// Loan Offer
public type LoanOffer = {
providerid: Text;
providername: Text;
applicationid: Int;
apr: Float;
created: Int;
};The canister has one method: apply.
To call this canister the annotated proxy Java interface LoanProxy.java and ProxyBuilder will be used.
LoanApplication and LoanOffer Java classes with the JAXB annotation are defined in LoanApplication.java and LoanOffer.java.
The example uses the file with XML LoanApplication payload as an input.
Next, create LoanApplication object using the JAXB Unmarshaller.
By default, JAXBSerializer and JAXBDeserializer will use Java to Candid type mapping and use the Java member variable name as the name of the Candid RECORD field.
Use JAXB annotations @XmlElement, @XmlAttribute or @XmlEnumValue to override the default name.
To skip serialization and deserialization of certain member variables, use the @XmlTransient annotation.
ProxyBuilder will use JAXBSerializer and JAXBDeserializer defined in Java Candid Serializer and Deserializer annotations to convert Candid RECORD to a Java JAXB object.
To use Raw methods or QueryBuilder and UpdateBuilder use JAXBSerializer and JAXBDeserializer directly in the Java code.
Last updated