How to generate *.xjb file from WSDL?
Asked Answered
R

3

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

Rete answered 26/3, 2015 at 23:36 Comment(0)
A
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>
Audacious answered 27/3, 2015 at 13:36 Comment(3)
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 ?Rete
@TitoCheriachan As an additional reading: JAXB binding: why customize?Zamudio
If there is a case where you have two elements with the same name.Audacious
F
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>
Ford answered 19/8, 2017 at 14:15 Comment(0)
C
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.
Cheesewood answered 28/9, 2020 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.