Question

I use CXF to generate java code from a 3rd party wsdl (not maintained by us).

I have this two snippets working not as expected when marshalling to soap message.

XML:
<choice>
  <sequence>
    <element name="x" type="xt" nillable="true" minOccurs="0" />
  </sequence>
  <element name="y" type="yt" 
     nillable="true" minOccurs="0" />
</choice>

Java:
@XmlElement(name = "x", nillable = true)
protected Xx;
@XmlElement(name = "y", nillable = true)
protected Y y;

If I add x, but not y to the soap message (via code) then it looks like:

<x>123456782</x>
<y xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:nil="true" />

Why is it adding y to the soap message, while it's an optional element?

Something similar is happening with this snippet:

XML:
<attribute ref="z" use="required" fixed="XXX" />

Java:
@XmlAttribute(name = "z", namespace = "http://www.egem.nl/StUF/StUF0301",
  required = true)
protected String z;

Here cxf (with schema validation on true) complains if I don't add the element via code. If i turn of the schema validation then is not creating the element add all.

This element is required and has a fixed attribute, why isn't it creating it if i don't add it (via code)?

EDIT: During development i realized that cxf was generating a .package-info.java file, which gave me trouble with namespaces. So I was searching for a way disable the generation of .package-info.java file. The solution for that was to put an extraarg to cxf codegen plugin: -xjc-npa in the pom file. That part in the pom looks like:

<plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-codegen-plugin</artifactId>
    <version>${cxf.version}</version>
    <configuration>
        <defaultOptions>
            <extraargs>                 
                <extraarg>-xjc-npa</extraarg>
            </extraargs>
        </defaultOptions>
    </configuration>
</plugin>

But to my surprise after re-generating the class files, was that all fixed elements are now constants. No more getters/setters generated. But unfortunately the problem with the choices remains.

Was it helpful?

Solution

Apparently i was reading old documentation. All I had to do was adding the below attributes to jaxb:globalBindings in the binding file.

In the old documentation there was no mention of these attributes.

<jaxb:globalBindings fixedAttributeAsConstantProperty="true" choiceContentProperty="true">

With these attributes all fixed attributes in the xml are now enums and all choice elements in the xml are now better generated in the code (in the xxxOrxxxOrxxx format).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top