Paulius Matulionis
Paulius Matulionis

Reputation: 23415

JAXB generating JAXBElement<String> instead of String

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;

Upvotes: 40

Views: 42603

Answers (6)

Reema
Reema

Reputation: 80

I created a file named bindings.xml in my recourses folder, and added the following :

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" elementFormDefault="qualified" attributeFormDefault="unqualified" jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.1">
    <xs:annotation>
        <xs:appinfo>
            <jaxb:globalBindings>
                <xjc:simple /><!-- it did only work after adding this -->
            </jaxb:globalBindings>
        </xs:appinfo>
    </xs:annotation>
</xs:schema>

Then I mentioned this file in my cxf plugin definition in the pom.xml as in :

in <wsdlOptions> tag , in <wsdlOption> where it has the path for the wsdl file, I added a tag which has:

<bindingFiles> 
 <bindingFile> ${basedir}/src/min/resources/bindings.xml </bindingFile>  
</bindingFiles>

My stubs got generated so prettily with actual types.

Upvotes: 2

Zineta Husic
Zineta Husic

Reputation: 1

I have had duplicated elements in my wsdl schema, that is why CXF generated JAXBElement instead of string. Be careful.

Upvotes: 0

Oleg Ushakov
Oleg Ushakov

Reputation: 1545

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

Upvotes: 0

Paulius Matulionis
Paulius Matulionis

Reputation: 23415

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.

Upvotes: 50

Frank Pavageau
Frank Pavageau

Reputation: 11705

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>

Upvotes: 6

ramsinb
ramsinb

Reputation: 2005

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

Upvotes: 13

Related Questions