Use and to serialize and deserialize 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 .
This example uses Motoko to call the canister . The canister uses 2 complex types, LoanApplication and LoanOffer.
public shared (msg) func apply(input : LoanApplication) : async LoanOffer {
Debug.print("Loan Application for user " #Principal.toText(msg.caller));
let offer : LoanOffer = {
providerid = Principal.toText(Principal.fromActor(this));
providername = "Loan Provider";
applicationid = 1;
apr = 3.14;
created = Time.now();
};
return offer;
};
LoanProxy.java
public interface LoanProxy {
@UPDATE
@Name("apply")
@Deserializer(JAXBDeserializer.class)
@Waiter(timeout = 30)
public CompletableFuture<LoanOffer> apply(@Serializer(JAXBSerializer.class) @Argument(Type.RECORD) LoanApplication loanApplication);
}
LoanApplication.java
@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;
}