6

I have an autogenerated soap webservice client (using cxf), and some elements are marked to be optional.

If I do not set these elements, the XML request send to the webservice has lot's of elements as follows:

<PayText xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<Name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>

These are generated if the wsdl contains:

minOccurs="0" nillable="true"

How can I prevent the generation of these nil elements? Probably the webservice itself does not need this information, as when I use soapUI and the send the pure xml requests stripping out the nil elements, the request still works.

My binding file:

<jaxb:globalBindings generateElementProperty="false" />

So, how can I prevent them being generated during send?

membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • Possible duplicate (at least a relevant question with answer): http://stackoverflow.com/questions/5897785/jaxb-marshaller-always-writes-xsinil-even-when-xmlelementrequired-false-nil – Davio Sep 30 '14 at 13:36
  • 1
    Not really. I have neither control of the `wsdl` (I'm just the client), nor on the autogenerated classes (as they must be regenerated every time a wsdl update takes place). – membersound Sep 30 '14 at 13:38
  • Well, in that case you're probably out of luck because of the 'nillable=true' which is present in the WSDL. – Davio Sep 30 '14 at 13:42
  • Did you find any solution to this problem? – Cleankod Nov 18 '16 at 15:06

1 Answers1

1

If an element is minOccurs="0" and nillable="true" then the generated property type will be a JAXBElement, something like JAXBElement<String>. When that property is null it will be excluded from the marshalled XML (null corresponds to minOccurs="0"). To get xsi:nil="true" you need to have an instance of JAXBElement with nil set to true.

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • So it is due to my ``? I want to prevent working with `JAXBElement`, but have the property directly, that's why I work with `generateElementProperty = false`. It it then impossible to remove the nil elements from the marshalled xml, as they are generated as consequence of the property?? – membersound Sep 30 '14 at 14:06
  • 1
    This is one of the cases where `JAXBElement` is useful. Without it the property will be annotated with `@XmlElement(nillable=true)` will cause the behaviour you are seeing. Why don't you want to use `JAXBElement`? – bdoughan Sep 30 '14 at 14:27
  • 2
    I'm preventing the use of jaxbelement to have one less element property to walk when accessing the autogenerated element properties. Could it be generally said if JAXBElement should be used in a project or not? – membersound Sep 30 '14 at 14:31
  • 2
    And also, I just can create the classes themself, not having to go through the way `new ObjectFactory().createTheElement("test");`. – membersound Sep 30 '14 at 14:43