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.
@XmlRootElement(name = "data", namespace="http://ic4j.org/samples")
public class LoanApplication{
@XmlElement(name="id", namespace="http://ic4j.org/samples", required=true)
public BigInteger id;
@XmlElement(name="amount", namespace="http://ic4j.org/samples", required=true)
public Double amount;
@XmlElement(name="term", namespace="http://ic4j.org/samples", required=true)
public Short term;
@XmlElement(name="firstname", namespace="http://ic4j.org/samples", required=true)
public String firstName;
@XmlElement(name="lastname", namespace="http://ic4j.org/samples", required=true)
public String lastName;
@XmlElement(name="ssn", namespace="http://ic4j.org/samples", required=true)
public String ssn;
@XmlElement(name="zipcode", namespace="http://ic4j.org/samples", required=true)
public String zipcode;
@XmlElement(name="created", namespace="http://ic4j.org/samples", required=true)
public BigInteger created;
}
LoanOffer.java
@XmlRootElement(name = "data", namespace="http://ic4j.org/samples")
public class LoanOffer{
@XmlElement(name="providerid", namespace="http://ic4j.org/samples", required=true)
public String providerId;
@XmlElement(name="providername", namespace="http://ic4j.org/samples", required=true)
public String providerName;
@XmlElement(name="userid", namespace="http://ic4j.org/samples", required=true)
public Principal userId;
@XmlElement(name="applicationid", namespace="http://ic4j.org/samples", required=true)
public Integer applicationId;
@XmlElement(name="apr", namespace="http://ic4j.org/samples", required=true)
public Double apr;
@XmlElement(name="created", namespace="http://ic4j.org/samples", required=true)
public Long created;
}
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.
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.