Create a SOAP client using the JAX-WS Maven Plugin

April 8th, 2010 by

Having 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 ;)

 

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

<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>

Tags: , , , , , , , , ,

13 Responses to “Create a SOAP client using the JAX-WS Maven Plugin”

  1. Stuffman Says:

    Great article, but the command is “mvn jaxws:wsimport” not just “jaxws:import”.

  2. micha kops Says:

    thanks for mentioning! *article updated* :)

  3. Steve Says:

    Your blogs- are very unique and interesting.

  4. Vlad Says:

    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?

  5. micha kops Says:

    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

  6. saad Says:

    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

  7. micha kops Says:

    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?

  8. mark Says:

    Dear
    How do I do for to start the client?
    From web?
    I don’t see the main method
    Thanks

  9. micha kops Says:

    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.

  10. Sricharan Says:

    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.

  11. micha kops Says:

    Hi Sricharan,

    I’m glad it worked for you – I know this situation well by myself :)

    Cheers,

    Micha

  12. metalfreak Says:

    Are you sure that artifact jaxws-tools should be placed in tag?

  13. metalfreak Says:

    I meant build tag. I’m sure I’ve written this but somehow it hasn’t appeared on the page.:/

Search
Categories