JAXB objects initialized with default values

前端 未结 2 1428
太阳男子
太阳男子 2020-12-11 02:58

There is little problem with JAXB.


Given:

  • Java 1.5; jaxb -jars from jaxws-2_0.
  • .xsd scheme and genera
2条回答
  •  情书的邮戳
    2020-12-11 03:38

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

    
       
       
       
      
    

    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

提交回复
热议问题