This tutorial will show you how we can publish and consume SOAP based JAX-WS webservice using maven wsgen and wsimport plugin. For this tutorial we will create two maven projects – first will be an web project and second one will be a standalone project in Eclipse.
If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.
Prerequisites
The following configurations are required in order to run the application
Eclipse Kepler
JDK 1.7
Tomcat 7
Have maven installed and configured
JAX-WS dependencies in pom.xml
Now we will see the below steps how to create a maven based spring project in Eclipse
First we will create service project
Step 1. Create a maven based web project in Eclipse
Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.
maven-arctype-webapp
Now enter the required fields (Group Id, Artifact Id) as shown below
Group Id : com.roytuts
Artifact Id : soapservice
Step 2. Modify the pom.xml file as shown below.
<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.roytuts</groupId> <artifactId>soapservice</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>soapservice Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <jdk.version>1.7</jdk.version> <jaxws.version>2.1.3</jaxws.version> <junit.version>4.11</junit.version> </properties> <dependencies> <dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-rt</artifactId> <version>${jaxws.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>soapservice</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxws-maven-plugin</artifactId> <version>1.12</version> <executions> <execution> <phase>process-classes</phase> <goals> <goal>wsgen</goal> </goals> </execution> </executions> <configuration> <sei>com.roytuts.soap.service.HelloServiceImpl</sei> <genWsdl>true</genWsdl> <keep>true</keep> <resourceDestDir>${basedir}/src/main/webapp/WEB-INF/wsdl</resourceDestDir> <sourceDestDir>${basedir}/src/main/java</sourceDestDir> </configuration> </plugin> </plugins> </build> </project>
Step 3. If you see JRE System Library[J2SE-1.4] then change the version by below process
Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.4], click on Edit button and select the appropriate jdk 1.7 from the next window. Click on Finish then Ok.
Step 4. Create below HelloServiceImpl.java
package com.roytuts.soap.service; import javax.jws.WebService; @WebService public class HelloServiceImpl { public String sayHello() { return "Hello"; } }
Step 5. Modify web.xml file as below under WEB-INF folder
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>SOAP Web Service</display-name> <listener> <listener-class> com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class> </listener> <servlet> <servlet-name>HelloService</servlet-name> <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloService</servlet-name> <url-pattern>/HelloService</url-pattern> </servlet-mapping> </web-app>
Step 6. Create sun-jaxws.xml file with the below content under WEB-INF folder
<?xml version="1.0" encoding="UTF-8"?> <endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0"> <endpoint name="HelloService" implementation="com.roytuts.soap.service.HelloServiceImpl" url-pattern="/HelloService" /> </endpoints>
Step 7. Now navigate to the project location from command prompt and build using maven tool by executing the below command
mvn clean compile install
Step 8. Now run or deploy the project into Tomcat server
Step 9. Hit the below URL and you will see the output in the browser
http://localhost:8080/soapservice/HelloService

Step 10. Click on the below URL to see the WSDL content
http://localhost:8080/soapservice/HelloService?wsdl

Now we will create a client project for consuming the service
Step 1. Create a standalone maven project in Eclipse
Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.
maven-arctype-quickstart
Now enter the required fields (Group Id, Artifact Id) as shown below
Group Id : com.roytuts
Artifact Id : soapclient
Step 2. Modify the pom.xml file as shown below.
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.roytuts</groupId> <artifactId>soapclient</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>soapclient</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <jdk.version>1.7</jdk.version> <junit.version>4.11</junit.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>soapservice</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxws-maven-plugin</artifactId> <version>1.12</version> <executions> <execution> <goals> <goal>wsimport</goal> </goals> <configuration> <wsdlUrls> <wsdlUrl>http://localhost:8080/soapservice/HelloService?wsdl</wsdlUrl> </wsdlUrls> <keep>true</keep> <packageName>com.roytuts.soap.client</packageName> <sourceDestDir>${basedir}/src/main/java</sourceDestDir> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.5</version> <executions> <execution> <id>add-source</id> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>${basedir}/src/main/java</source> </sources> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
Step 3. If you see JRE System Library[J2SE-1.4] then change the version by below process
Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.4], click on Edit button and select the appropriate jdk 1.8 from the next window. Click on Finish then Ok.
Step 4. Navigate to the project location and build the project using maven based tool by executing the below command in command prompt.
Note that the service project (soapservice) must be up and running.
mvn clean compile install
Step 5. Create a Java main class to test the service
package com.roytuts.soap.client; public class HelloClient { public static void main(String[] args) { HelloServiceImplService helloService = new HelloServiceImplService(); HelloServiceImpl helloServiceImpl = helloService.getHelloServiceImplPort(); String response = helloServiceImpl.sayHello(); System.out.println(response); } }
Step 6. Now run the HelloClient.java and you will get the below output in the console.
Hello
That’s all. Thanks for reading.