This post demonstrates how to generate Java classes from WSDL files using cfx-codegen-plugin. This means we require Maven for this purpose.
Software Requirements
The following items were used for this post.
- Java 8
- Maven
- IntelliJ IDEA 2017.2.6
- cxf-xjc-runtime
- See pom.xml
WSDL Source
We will use a WSDL file downloaded from the following URL
http://www.webservicex.net/geoipservice.asmx?WSDL
The web service may change anytime without us knowing it unless we consume the it. Therefore, we included the WSDL file in our Maven project.
Non-Standing SOAP 1.2 Binding
You may encounter the following issue with wsimport. It may still work with older version of SOAP. If possible, please use wsdl2java directly or via cxf-codegen-plugin.
We will only deal with SOAP 1.2 in this post.
Maven Plugin
The key configuration to achieve our end to update our pom.xml as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
... <build> <plugins> <plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin</artifactId> <version>3.2.1</version> <executions> <execution> <id>generate-sources</id> <phase>generate-sources</phase> <configuration> <wsdlOptions> <wsdlOption> <wsdl>${project.basedir}/src/main/resources/wsdl/geoipservice.wsdl</wsdl> <wsdlLocation>classpath:wsdl/geoipservice.wsdl</wsdlLocation> <extraargs> <extraarg>-p</extraarg> <extraarg>com.turreta.wsdl.webservicex.geoipservice</extraarg> <extraarg>-bareMethods</extraarg> </extraargs> <frontEnd>jaxws21</frontEnd> <faultSerialVersionUID>1</faultSerialVersionUID> <bindingFiles> <bindingFile>src/main/resources/binding.xml</bindingFile> </bindingFiles> </wsdlOption> </wsdlOptions> </configuration> <goals> <goal>wsdl2java</goal> </goals> </execution> </executions> </plugin> ... </plugins> </build> ... |
We also need the following dependency.
1 2 3 4 5 |
<dependency> <groupId>org.apache.cxf.xjc-utils</groupId> <artifactId>cxf-xjc-runtime</artifactId> <version>2.7.0</version> </dependency> |
When we do a clean install, we get the following generated classes along with the .jar file.
Check the Jar file
There is another plugin (in the same pom.xml) responsible in moving the generated compiled classes to our jar file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
... <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.9</version> <executions> <execution> <id>add-source</id> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>${basedir}/target/generated/src/main/java</source> </sources> </configuration> </execution> </executions> </plugin> ... |
Download the codes
https://github.com/Turreta/Generate-Java-classes-from-WSDL-files-using-cxf-codegen-plugin-in-Maven
Recent Comments