7

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

informatik01
  • 16,038
  • 10
  • 74
  • 104
Tito
  • 8,894
  • 12
  • 52
  • 86

3 Answers3

9

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>
informatik01
  • 16,038
  • 10
  • 74
  • 104
  • you mean to say , creating an xjb manually is only to resolve conflicts and and the classes generated from the wsdl are good to go ? – Tito Mar 27 '15 at 17:38
  • 1
    @TitoCheriachan As an additional reading: [JAXB binding: why customize?](http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.5/tutorial/doc/JAXBUsing4.html#wp148224) – informatik01 Mar 09 '17 at 16:12
  • 1
    If there is a case where you have two elements with the same name. – Surendra Poranki Mar 16 '17 at 17:39
1

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>
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
anjan
  • 11
  • 1
0

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.
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277