I use the jaxb2-maven-plugin to generate JaxB-Classes from a given XSD. It works fine. But now I want to use java.util.Optional in the generated Classes. But JaxB generated the Classes without the Optionals. So I'm forced to make a Null-Check of every Variable.
Did anybody know how the jaxb2-maven-plugin has to be configured to use the java.util.Optional?
Thanks for you help!
After the Java artifacts for your application are generated, you can generate fully annotated Java classes from an XML schema file by using the JAXB schema compiler, xjc command-line tool.
The JAXB compiler generates Java classes that map to constraints in the source XML schema. The classes implements get and set methods that you can use to obtain and specify data for each type of element and attribute in the schema. Process XML documents by instantiating the generated classes in a Java program.
xsd is the XML schema you will use as input to the JAXB binding compiler, and from which schema-derived JAXB Java classes will be generated.
Maybe you could find something more generic but i'm not sure if this is possible. Anyway you can still define custom Adapter for types you want to be optional.
Here is an example of Integer
First, create an Adapter
public final class IntegerOptionalAdapter extends OptionalAdapter<Integer>
{
}
Then use this adapter in your binding
@XmlAttribute
@XmlJavaTypeAdapter(IntegerOptionalAdapter.class)
private Optional<Integer> someInteger;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With