34

I am using Apache CXF cxf-codegen-plugin Maven plugin to generate sources from WSDL file. Problem is that I get JAXBElement<String> generated instead of String. I have added the jaxb-bindings.xml file which looks like this:

<jaxb:bindings version="2.1"
               xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
    <jaxb:globalBindings generateElementProperty="false"/>
</jaxb:bindings>

This should prevent JAXB to generate JAXBElement<String>. But it is not working I still have JAXBElement<String> generated instead of String.

My Maven plugin looks like this:

<plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-codegen-plugin</artifactId>
    <version>${cxf.runtime.version}</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-bindings-soap</artifactId>
            <version>${cxf.runtime.version}</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <id>generate-jaxb</id>
            <phase>generate-sources</phase>
            <configuration>
                <additionalJvmArgs>-Dfile.encoding=UTF8</additionalJvmArgs>
                <wsdlOptions>
                    <wsdlOption>
                        <wsdl>src/main/resources/wsdl/Cubiks.wsdl</wsdl>
                        <extraargs>
                            <extraarg>-b</extraarg>
                            <extraarg>${basedir}/jaxb-bindings.xml</extraarg>
                            <extraarg>-b</extraarg>
                            <extraarg>${basedir}/jaxws-bindings.xml</extraarg>
                            <extraarg>-exsh</extraarg>
                            <extraarg>true</extraarg>
                            <extraarg>-wsdlLocation</extraarg>
                            <extraarg></extraarg>
                        </extraargs>
                    </wsdlOption>
                    <wsdlOption>
                        <wsdl>src/main/resources/wsdl/CubiksCallBackService.wsdl</wsdl>
                        <extraargs>
                            <extraarg>-b</extraarg>
                            <extraarg>${basedir}/jaxws-bindings.xml</extraarg>
                            <extraarg>-b</extraarg>
                            <extraarg>${basedir}/jaxb-bindings.xml</extraarg>
                            <extraarg>-exsh</extraarg>
                            <extraarg>true</extraarg>
                            <extraarg>-p</extraarg>
                            <extraarg>com.cubiks.ws.callback</extraarg>
                            <extraarg>-wsdlLocation</extraarg>
                            <extraarg></extraarg>
                        </extraargs>
                    </wsdlOption>
                </wsdlOptions>
            </configuration>
            <goals>
                <goal>wsdl2java</goal>
            </goals>
        </execution>
    </executions>
</plugin>

CXF version is 2.6.0. Does someone know where might be the problem?

EDIT

The XSD is very huge. This is the element which generating JAXBElement<String>

  <xs:complexType name="ServiceResponse">
    <xs:sequence>
      <xs:element minOccurs="0" name="RequestStatus" type="tns:RequestStatus"/>
      <xs:element minOccurs="0" name="RequestStatusDescription" nillable="true" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="ServiceResponse" nillable="true" type="tns:ServiceResponse"/>

And the generated source is:

@XmlElementRef(name = "RequestStatusDescription", namespace = "http://www.cubiksonline.com/2009/08/AssessmentProvider", type = JAXBElement.class)
protected JAXBElement<String> requestStatusDescription;
Paulius Matulionis
  • 21,481
  • 21
  • 97
  • 139

4 Answers4

44

What I had to do is to wrap jaxb:globalBindings with another jaxb:bindings.

<jaxb:bindings version="2.0"
               xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
    <jaxb:bindings>
        <jaxb:globalBindings generateElementProperty="false"/>
    </jaxb:bindings>
</jaxb:bindings>

Now everything is working, there is no JAXBElement<String> generated anymore.

Paulius Matulionis
  • 21,481
  • 21
  • 97
  • 139
  • Happy to report this solution works in JDeveloper as well. Just create an xml file then pass it in the wizard at the phase when you specify the URL/location of the WSDL. – webuster Feb 11 '16 at 15:22
  • This worked in NetBeans 8.2 as well. Right click on the service and "edit web service attributes". Then go to "WSDL customization" tab and at the bottom under "External Binding Files" add this file and regenerate. All Strings resolved as String. Tanks! – Darrel K. Nov 24 '17 at 08:48
11

You can't have nillable and minoccurs together. Remove the minoccurs as it doesn't make sense for strings anyway.

ramsinb
  • 1,957
  • 12
  • 16
  • And why is that I can't have them together? By the way the wsdl file is provided by the client, we didn't create it. I understand that they does not make sense, but it its not for me to decide to remove them or not. – Paulius Matulionis Sep 20 '12 at 09:03
  • 6
    If the schema has both then you need a JAXBElement to be able to distinguish between the cases where the XML element is missing (allowed by minOccurs) and where it is present but `xsi:nil`. If the schema only allowed one or the other then you wouldn't need the JAXBElement as a `null` property value would be enough. – Ian Roberts Sep 20 '12 at 09:20
  • 3
    [This post](http://cxf.547215.n5.nabble.com/JAXBElement-in-generated-java-classes-tp4933735p4934002.html) explains it nicely. – Ian Roberts Sep 20 '12 at 09:22
  • Well what does it mean for a string to be nillable and have minoccurs=0? What would the XML look like if those condition were met? Anyway have a look at [here](http://cxf.apache.org/docs/maven-cxf-codegen-plugin-wsdl-to-java.html) to make sure you are using the binding correctly there is an example Showing how to configure the maven plugin. Your binding file is correct though I suspect if you configure your maven plugin correctly it will work. Since you can't change the wsdl this is your only option. – ramsinb Sep 20 '12 at 09:24
  • That's odd, I wrapped my globalBindings with jaxb:bindings and it worked. I still can't understand why. – Paulius Matulionis Sep 20 '12 at 09:38
7

I think you want to add in your jaxb-binding.xml:

<jaxb:bindings ... xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">
    <jaxb:globalBindings generateElementProperty="false">
        <xjc:simple />
        <!-- ... -->
    </jaxb:globalBindings>
</jaxb:bindings>
Frank Pavageau
  • 10,797
  • 1
  • 43
  • 49
0

When we have both minouucrs=0 and nillable=true in XSD it will generate JAXBElement. To avoid this we need to remove either of the one.

If you use generateElementProperty="false" property when generating POJO objects from XSD scheme then it will be working like a charm

Oleg Ushakov
  • 388
  • 3
  • 15