简体   繁体   中英

JAXB xmlelement default value for boolean flag

I have a JAXB class as shown below which is used as a parameter in POST method . Its having a parameter called isTrue which is an optional parameter,the default value of the parameter should be true when its not passed in the REST request. But when i am trying to invoke the method and not passing it the default value of set as false . can anyone suggest me what could be the cause of the issue?i need true as the value when i am not passing any value for isTrue . I am using REST webservices using jersey.

@XmlRootElement(name = "param")
public class Param {
     private boolean isTrue;

     @XmlElement(required = false, name = "isTrue", defaultValue = "true")
     public boolean isTrue() {
        return isTrue;
     }

     public void setTrue(boolean isTrue) {
        this.isTrue = isTrue;
     }
}

Thanks in advance

This is actually a known issue, refer to https://java.net/projects/jaxb2-commons/pages/Default-Value

Basically the article above describes a JAXB plugin that explicitly initializes default boolean variables to true, if desired. Since you are not generating JAXB code and writing it manually, you can just explicitly initialize the parameter to true:

private boolean isTrue = true;

This is what the plugin referenced above does as a solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM