I have a boolean field called a and two methods void setA(String a) and boolean isA(). I have set @XmlAccessorType(XmlAccessType.NONE) and used @XmlAttribute for the setter.
Because the getter returns a boolean value but the setter expects a string JAX-B just ignores this setter. This is the cause for all kinds of bugs in the code because boolean values are not set correctly and debugging that is very annoying.
Is there a way to tell JAX-B to use the setter? Why is JAX-B confused by the getter method at all, I though using XmlAccessType.NONE prevents all that implicit interpreting?
Plan B would be to let JAX-B fails if such a constellation appears, but how can this be done?
Thankful for any hint 🙂
I would recommend using
@XmlAccessType.FIELDas suggested by Kevin combined with anXmlAdapterto get the behaviour you are looking for:Root
To get this example to work with the JAXB-RI I need to make the field of type
Boolean. If you are using EclipseLink JAXB (MOXy) then you can make the fieldboolean.BooleanAdapter
The
XmlAdapteris where you can add the logic that you have in yoursetA(String)method.Demo
input.xml
Output
UPDATE
Alternatively you could introduce a
Stringgetter for theaproperty. You would need to make theisA()method as@XmlTransient: