JAXB objects initialized with default values

23,982

Solution 1

default value that is in annotations works only after unmarshalling.
unmarshal this

<document>
   <d_int/>
   <d_double/>
   <d_string/>
</document>  

and you will get object with default values (-1, -1.0, "Default")

If you want set default values to marshalling, you should do this

public class Document {
    @XmlElement(name = "d_int", defaultValue = "-1")
    protected int dInt = 100;
    @XmlElement(name = "d_double", defaultValue = "-1.0")
    protected double dDouble = -100;
    @XmlElement(name = "d_string", required = true, defaultValue = "Default")
    protected String dString = "def";
...
}    

jaxb generate default values only for unmarshalling

Solution 2

For an initialization of the class members from XSD supplied defaults, you can use the default-value-plugin of XJC.

See the website of the plugin.

Note that the ant task definition as explained in that documentation did not work for me. As explained here, the class paths for XJC and the plugin have to be separated. Specifying the path to the plugin when calling it works for me:

<xjc schema="some.xsd" >
    <arg value="-Xdefault-value"/>
    <classpath>
        <pathelement location="lib/xjc-plugins/jaxb2-default-value-1.1.jar"/>
    </classpath>
</xjc>
Share:
23,982
A7iz
Author by

A7iz

Updated on May 01, 2020

Comments

  • A7iz
    A7iz about 4 years

    There is little problem with JAXB.


    Given:

    • Java 1.5; jaxb -jars from jaxws-2_0.
    • .xsd scheme and generated JAXB classes.
    • Every simple element in .xsd has default value. And as result class members has annotations like "@XmlElement(name = "cl_fname", required = true, defaultValue = "[______]")"

    Required


    Get java object (root element) which fully represent xml and every member initialized by default values.


    when I try to marshall xml without explicitly setting values, default values doesn't make sence... is there any way to marshall xml populated with default values without customization of generated classes?

    example of .xsd:

    <xs:element name="document">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="d_int"/>
                <xs:element ref="d_double"/>
                <xs:element ref="d_string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    
    <xs:element name="d_int" type="xs:int" default="-1"/>
    <xs:element name="d_double" type="xs:double" default="-1.0"/>
    <xs:element name="d_string" type="xs:string" default="false"/>
    

    and java class:

    public class Document {
        @XmlElement(name = "d_int", defaultValue = "-1")
        protected int dInt;
        @XmlElement(name = "d_double", defaultValue = "-1.0")
        protected double dDouble;
        @XmlElement(name = "d_string", required = true, defaultValue = "Default")
        protected String dString;
    ...
    }
    
  • bvdb
    bvdb about 7 years
    I've been wondering, is there any point in marking a field as required if you also specify a defaultValue. That seems to contradict, doesn't it ?
  • mapeters
    mapeters over 6 years
    If you leave off the defaultValue, will the value assigned to the field be the default both for unmarshalling and marshalling, or will there be no default value for unmarshalling? (e.g. if you took off defaultValue = "-1" for dInt, would 100 be the default value for unmarshalling also?)