How to generate *.xjb file from WSDL?

18.3k Views Asked by At

I have a WSDL file weatherservice.wsdl and I am trying to generate binding for this WSDL using xjc. How do I do this using xjc?

I did not find any command line args to do it from xjc. xjc -p com -wsdl weatherservice.wsdl

3

There are 3 best solutions below

3
On BEST ANSWER

Generally we create a bindings file with .xjb extension to resolve any conflicts in the WSDL or schema. For example if two elements have the same name and you want to distinguish between them you can rename one by specifying it the bindings file.

Here is an example:

<jaxb:bindings 
    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"
    jaxb:extensionBindingPrefixes="xjc"
    version="2.1">

    <jaxb:globalBindings generateIsSetMethod="true" fixedAttributeAsConstantProperty="true">
        <xjc:serializable uid="1" />
    </jaxb:globalBindings>

    <jaxb:bindings schemaLocation="abcd.xsd">
        <jaxb:bindings node="//xs:element[@name='Event']/xs:simpleType">
               <jaxb:typesafeEnumClass name="EventEnumType" />
        </jaxb:bindings>
   </jaxb:bindings>

</jaxb:bindings>
0
On

You could do it through the Maven plugin, which simplifies a bit, and complicates a bit.

This will take a few XSDs and apply the XJBs to generate the Java classes:

                <!-- Docs:
                        Maven plugin: http://cxf.apache.org/cxf-xjc-plugin.html
                        XJC: https://javaee.github.io/jaxb-v2/doc/user-guide/ch04.html
                        https://tech.boldare.com/make-jaxb-great-again/
                -->
                <plugin>
                    <groupId>org.apache.cxf</groupId>
                    <artifactId>cxf-xjc-plugin</artifactId>
                    <version>${version.cxf-xjc}</version>
                    <configuration>
                        <sourceRoot>${basedir}/target/generated-sources/main/java</sourceRoot>
                        <xsdOptions>
                            <xsdOption>
                                <extension>true</extension>
                                <xsd>${xsdsBaseDir}/someService/some-soap-service.xsd</xsd>
                                <!-- Args to XJC execution -->
                                <extensionArgs>
                                    <extensionArg>-XautoNameResolution</extensionArg>
                                    <extensionArg>-encoding</extensionArg><extensionArg>UTF-8</extensionArg>
                                </extensionArgs>
                            </xsdOption>
                            <xsdOption>
                                <!-- another XSD... -->
                            </xsdOption>
                        </xsdOptions>

                        <extensions>
                            <extension>org.apache.cxf.xjcplugins:cxf-xjc-wsdlextension:${version.cxf-xjc}</extension>
                            <extension>org.apache.cxf.xjcplugins:cxf-xjc-boolean:${version.cxf-xjc}</extension>
                            <extension>org.apache.cxf.xjcplugins:cxf-xjc-ts:${version.cxf-xjc}</extension>
                            <extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:${version.cxf-xjc}</extension>
                            <extension>org.apache.cxf.xjcplugins:cxf-xjc-bug671:${version.cxf-xjc}</extension>
                            <extension>org.apache.cxf.xjcplugins:cxf-xjc-bug986:${version.cxf-xjc}</extension>
                            <extension>org.apache.cxf.xjcplugins:cxf-xjc-javadoc:${version.cxf-xjc}</extension>
                            <!--
                            -->
                        </extensions>
                    </configuration>
                    <executions>
                        <execution>
                            <id>generate-from-xsds</id><phase>generate-sources</phase><goals><goal>xsdtojava</goal></goals>
                        </execution>
                    </executions>
                </plugin>

Two gotchas:

  1. The plugin can't take defaults from the execution config - must be set in the plugin-level config.
  2. You'll need to add the sources to the reactor, if you have some tests of the schema within the same module.
0
On

Try using this simple binding. Save it next to the WSDL and tell XJC to use that.

<bindings version="2.0" 
          xmlns="http://java.sun.com/xml/ns/jaxb"
          xmlns:xs="http://www.w3.org/2001/XMLSchema" 
          xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
>

    <globalBindings>
        <xjc:simple/>
    </globalBindings>

</bindings>