[ACCEPTED]-JAXB objects initialized with default values-default-value

Accepted answer
Score: 22

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


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

If 3 you want set default values to marshalling, you 2 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 1 only for unmarshalling

Score: 1

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

See the 5 website of the plugin.

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

<xjc schema="some.xsd" >
    <arg value="-Xdefault-value"/>
        <pathelement location="lib/xjc-plugins/jaxb2-default-value-1.1.jar"/>

More Related questions