JAXB generating JAXBElement<String> instead of String

37,784

Solution 1

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.

Solution 2

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

Solution 3

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>
Share:
37,784
Paulius Matulionis
Author by

Paulius Matulionis

Check some of my posts at: http://pauliusmatulionis.blogspot.co.uk/

Updated on February 10, 2022

Comments

  • Paulius Matulionis
    Paulius Matulionis over 2 years

    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
    Paulius Matulionis almost 12 years
    I am trying to add it but I am getting: com.sun.istack.SAXParseException2: The "xjc:simple" customization is not associated with any schema element.
  • Paulius Matulionis
    Paulius Matulionis almost 12 years
    I have managed to <xjc:simple /> correctly but still the same. JAXBElement<String> is generated.
  • Paulius Matulionis
    Paulius Matulionis almost 12 years
    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.
  • Frank Pavageau
    Frank Pavageau almost 12 years
    Right, I've now checked out a WS to test, it's the generateElementProperty="false" attribute which avoids the JAXBElement wrapper in my case. Sorry for the noise.
  • Ian Roberts
    Ian Roberts almost 12 years
    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
    Ian Roberts almost 12 years
    This post explains it nicely.
  • ramsinb
    ramsinb almost 12 years
    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 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.
  • Paulius Matulionis
    Paulius Matulionis almost 12 years
    That's odd, I wrapped my globalBindings with jaxb:bindings and it worked. <jaxb:bindings version="2.0" xmlns:jaxb="java.sun.com/xml/ns/jaxb"> <jaxb:bindings> <jaxb:globalBindings generateElementProperty="false"/> </jaxb:bindings> </jaxb:bindings> I still can't understand why.
  • webuster
    webuster over 8 years
    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.
  • Darrel K.
    Darrel K. over 6 years
    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!
  • Reema
    Reema over 2 years
    Where do you add this snippet ? @PauliusMatulionis