Create a SOAP client using the JAX-WS Maven Plugin
April 8th, 2010 by Micha KopsHaving written the article “How to build a Confluence SOAP client in 5 minutes” some readers asked me for some more information and help using the JAX-WS plugin that I mentioned in the article instead of the Axis plugin – so here we go ;)
Contents
Steps
- Create a simple maven project first using archetype:create or archetype:generate
mvn archetype:create -DgroupId=com.hascode.jaxws -DartifactId=soap-tutorial
- We get a pom.xml like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hascode.jaxws</groupId> <artifactId>soap-tutorial</artifactId> <version>0.1</version> </project>
- Insert plugin references and dependencies to your pom.xml
<dependencies> <dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-rt</artifactId> <version>2.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxws-maven-plugin</artifactId> <executions> <execution> <goals> <goal>wsimport</goal> </goals> </execution> </executions> <configuration> <packageName>com.hascode.schema</packageName> </configuration> </plugin> </plugins> </build>
- Specify the package name for the generated java files in the plugin configuration – I used com.hascode.schema – you might want to edit this ;)
- JAX-WS needs at least Java5 so add this source/target information to the build element
<plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin>
- If you wish to use a specific version of JAX-WS you may specify your needs adding this plugin configuration to the build element
<dependencies> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-tools</artifactId> <version>2.2.1</version> </dependencies>
- If maven cries about unresolved dependencies to com.sun.xml.ws-rt you should add the Sun Maven Repository to your pom.xml
<repositories> <repository> <id>maven2-repository.dev.java.net</id> <name>Java.net Repository for Maven</name> <url>http://download.java.net/maven/2</url> </repository> </repositories>
- In conclusion – my pom.xml looks like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hascode.jaxws</groupId> <artifactId>soap-tutorial</artifactId> <version>0.1</version> <dependencies> <dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-rt</artifactId> <version>2.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxws-maven-plugin</artifactId> <executions> <execution> <goals> <goal>wsimport</goal> </goals> </execution> </executions> <configuration> <packageName>com.hascode.schema</packageName> </configuration> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>maven2-repository.dev.java.net</id> <name>Java.net Repository for Maven</name> <url>http://download.java.net/maven/2</url> </repository> </repositories> </project>
- Create a directory named wsdl in your src directory – the WSDL files go there – the maven plugin searches for files matching *.wsdl in this directory
- Get a valid WSDL somewhere an store it in src/wsdl – I used a sample wsdl file from a IBM tutorial I once read for testing.
- Generate your java files using the jaxws-plugin:
mvn jaxws:wsimport
- Enjoy the generated files in target/jaxws/wsimport/java with the defined package name com.hascode.schema
Troubleshooting
- wsimport often needs some extra tuning to get some wsdl definitions going – so if you did not succeed try to configure the process in adjusting the plugin parameters, get one or more bottles of wine and parse through the jax-ws documentation or switch to axis and get the stuff running in minutes – nah just kidding :)
- In Eclipse you sometimes have to adjust the Java VM defined in the build path and the compiler target level (Project Properties > Java Compiler | Build Path)
- In Eclipse you can easily validate your stored wsdl file by opening it in the xml editor and selecting “Validate” in the context menu
- “[ERROR] undefined element declaration ‘xs:schema’” xs:schema is referenced but an import is missing – read this nice article from Vivek Pandey on Java.net how to fix this problem
Links
- Goal documentation: jaxws:wsimport
- Java.net: JAX-WS Maven Plugin
- Sun blog article: Using JAX-WS with Maven
- My article explaining the Axis-Maven-Plugin
- Oracle/Sun Jax-WS Homepage
- IBM.com: Developing a JAX-WS client from a WSDL file
- Axis Maven Plugin Homepage
- Vivek Pandey. Java.net: How to deal with unresolved schema references
<dependencies>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
<configuration>
<packageName>com.hascode.somepackage</packageName>
</configuration>
</plugin>
</plugins>
<build>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
<configuration>
<packageName>com.hascode.somepackage</packageName>
</configuration>
</plugin>
</plugins>
<build>
Tags: Axis, Axis2, jax-ws, maven, maven2, soap, tutorial, webservice, wsdl, wsimport
April 18th, 2012 at 8:16 pm
Great article, but the command is “mvn jaxws:wsimport” not just “jaxws:import”.
April 19th, 2012 at 7:21 pm
thanks for mentioning! *article updated* :)
August 7th, 2012 at 3:06 am
Your blogs- are very unique and interesting.
October 4th, 2012 at 2:03 pm
Is it possible to specify the URL of a WSDL for Maven to use, instead of downloading and installing the WSDL file in a wsdl directory?
October 8th, 2012 at 12:42 pm
I think it is possible – the jaxws-maven-plugin allows to specify a list of wsdl urls for the goal jaxws:wsimport: http://mojo.codehaus.org/jaxws-maven-plugin/wsimport-mojo.html
October 30th, 2013 at 11:47 am
Hello, great article
Need a help please.
I have a webservice generate by EJB3 and deploy in EAR.
I want to create just Stub with maven wsimport to use it in my WAR.
When i package the generated class with wsimport, put it in my war and excute the client : i have this error :
java.lang.LinkageError: loader constraint violation: when resolving field “DATETIME” the class loader (instance of org/jboss/classloader/spi/base/BaseClassLoader) of the referring class, javax/xml/datatype/DatatypeConstants, and the class loader (instance of ) for the field’s resolved type, javax/xml/namespace/QName, have different Class objects for that type
How to fix it or the best way the accomplish it.
thanks
October 30th, 2013 at 7:29 pm
I’m not sure – without more details it seems as if the class is loaded by two classloaders in your jboss instance .. you could try to debug the class loading using the vm parameter “-verbose:class” -> http://www.oracle.com/technetwork/java/javase/clopts-139448.html
Are you perhaps including a version of jax-rs in your war/ear?
May 21st, 2014 at 5:29 pm
Dear
How do I do for to start the client?
From web?
I don’t see the main method
Thanks
May 21st, 2014 at 8:07 pm
Hi,
the plugin should generate skeleton classes for you that allows you to open a connection the remote soap service.
Since the tutorial is four years old now, I could update it using the latest versions of the libraries used and with some downloadable sources included.
June 25th, 2014 at 5:35 am
Your article helped me a lot.I was struggling to get wsimport to work with maven for almost a day.Whichever website i tried failed.But Finally!!!!! it worked.Thanks a lot.
July 3rd, 2014 at 7:13 pm
Hi Sricharan,
I’m glad it worked for you – I know this situation well by myself :)
Cheers,
Micha
February 12th, 2015 at 7:15 pm
Are you sure that artifact jaxws-tools should be placed in tag?
February 12th, 2015 at 7:19 pm
I meant build tag. I’m sure I’ve written this but somehow it hasn’t appeared on the page.:/