jaxb-api XML Feld nicht gesendet setzt das Defaultvalue nicht

Wiplash4

Aktives Mitglied
Ich verwende https://mvnrepository.com/artifact/jml/jaxb-api/2.1 .
Java:
@XmlElement(defaultValue = "false")
protected Boolean help1;
@XmlElement(defaultValue = "false")
protected Boolean help2;
@XmlElement(defaultValue = "false")
protected Boolean help3;

Wenn ich nun per XML
XML:
<help1>false<help1>
<help2/>
<help3>true</help3>

sende, wird der Defaultvalue von 'help2' korrekt auf 'false' gesetzt. Wenn ich aber aber das Feld gar nicht sende
XML:
<help1>false<help1>
<help3>true</help3>

kommt als Resultat 'null'. Ich will aber auch hier, dass 'help2' auf false gesetzt wird.

Wie loese ich das?
 
Here is how element defaulting works in JAXB 2.0 as
extracted from JAXB 2.0 Specification, Public draft, Section 6.7.4 "Bind to a
Property"

Element defaulting
The default value is derived from the element declaration’s {value constraint}

property’s value. Unlike attribute defaulting, an element only defaults when
there is an empty element tag in an xml document
. The element’s default value is
captured by mapping annotation @XmlElement.defaultValue(). The unmarshaller sets
the property to this default value when it encounters an empty element tag. The
marshaller can output an empty element tag whenever the element’s @XmlValue
property value is the same as its defaulted value.

Du musst dein Dto dann so anlegen:
Java:
protected Boolean help2 = false;

Gehen müsste auch (aber das hab ich nicht getestet)
Java:
protected boolean help2;
 
Tatsaechlich habe ich es probiert. Und es funktioniert. Der Grund, warum es nicht funktionierte, war dass ich einen Fehler in den Project Versions in Maven gemacht habe.
Danke fuer den Hinweis.
 

Zurück
Oben