A Technology Blog About Code Development, Architecture, Operating System, Hardware, Tips and Tutorials for Developers.

Wednesday, November 28, 2012

Web Service Client

9:01:00 PM Posted by Satish , , 1 comment
We have done with Document Style Web Service, RPC Style Web Service, MTOM Web Service and integrated with web application in JAX-WS Web Integration (Glassfish Metro). Now it is time to write the client for them, yes we are going to consume the web services. I am going to demonsrate both jdk wsimport and maven plugin to import the web services from wsdl. In my last post JAX-WS Web Integration (Glassfish Metro), I have focused how the document and RPC style differ from each other while building web service. Here I am going to demonstrate the difference between RPC and Document style web services, while writing the client.

For this tutorial I will be using the following tools

1. JDK 7
2. Eclipse Juno
3. Maven2

We are going to use the same web services that we have demonstrated in JAX-WS Web Integration (Glassfish Metro). So before stating the tutorial let me deploy the war to the tomcat. So we will be doing the following things here:

1. Import web service and stub creation (using wsimport and maven wsimport plugin)
2. Client for Document style web service
3. Client for RPC style web service

Before start coding let's create a java project using the following maven command. 

1
mvn archetype:generate -DgroupId=com.techiekernel -DartifactId=webservice-client -Dpackagename=com.techiekernel

After executing, the project will be get created with pom.xml file. Bellow is the final pom.xml file with wsimport plugin.

 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<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.techiekernel</groupId>
 <artifactId>webservice-client</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>webservice-client</name>
 <url>http://maven.apache.org</url>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>
 </dependencies>
 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1.1</version>
   </plugin>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
     <source>1.5</source>
     <target>1.5</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>
     </execution>
    </executions>
    <configuration>
     <wsdlUrls>
      <wsdlUrl>http://localhost:8080/webservice-JAX-WS-Web-Metro/foobar?wsdl</wsdlUrl>
      <wsdlUrl>http://localhost:8080/webservice-JAX-WS-Web-Metro/foobarRPC?wsdl</wsdlUrl>
      <wsdlUrl>http://localhost:8080/webservice-JAX-WS-Web-Metro/fileImageServer?wsdl</wsdlUrl>
     </wsdlUrls>

     <sourceDestDir>src/main/java</sourceDestDir>
     <verbose>true</verbose>
    </configuration>
   </plugin>
  </plugins>
  <finalName>webservice-client</finalName>
 </build>
</project>

Import web service and stub creation (using wsimport and maven wsimport plugin):


We can import the web service using wsimport in the following way. Once you import the web services, you will notice the stub for document and RPC style web services are totally different. Same way the SOAP engine will take the responsibility of marshaling and demarshaling the SOAP request in case of RPC style.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
satish@sertao:~/code-base/code-playground/webservice-client$ wsimport -keep -verbose http://localhost:8080/webservice-JAX-WS-Web-Metro/foobar?wsdl -d src/main/java/
parsing WSDL...



Generating code...

com/techiekernel/ws/jaxws/document/CallFooBar.java
com/techiekernel/ws/jaxws/document/CallFooBarResponse.java
com/techiekernel/ws/jaxws/document/FooBarImpl.java
com/techiekernel/ws/jaxws/document/FooBarImplService.java
com/techiekernel/ws/jaxws/document/GetServerDetail.java
com/techiekernel/ws/jaxws/document/GetServerDetailResponse.java
com/techiekernel/ws/jaxws/document/ObjectFactory.java
com/techiekernel/ws/jaxws/document/Server.java
com/techiekernel/ws/jaxws/document/package-info.java

Compiling code...

javac -d /home/satish/code-base/code-playground/webservice-client/src/main/java -classpath /usr/lib/jvm/java-7-oracle/lib/tools.jar:/usr/lib/jvm/java-7-oracle/classes -Xbootclasspath/p:/usr/lib/jvm/java-7-oracle/jre/lib/rt.jar:/usr/lib/jvm/java-7-oracle/jre/lib/rt.jar /home/satish/code-base/code-playground/webservice-client/src/main/java/com/techiekernel/ws/jaxws/document/CallFooBar.java /home/satish/code-base/code-playground/webservice-client/src/main/java/com/techiekernel/ws/jaxws/document/CallFooBarResponse.java /home/satish/code-base/code-playground/webservice-client/src/main/java/com/techiekernel/ws/jaxws/document/FooBarImpl.java /home/satish/code-base/code-playground/webservice-client/src/main/java/com/techiekernel/ws/jaxws/document/FooBarImplService.java /home/satish/code-base/code-playground/webservice-client/src/main/java/com/techiekernel/ws/jaxws/document/GetServerDetail.java /home/satish/code-base/code-playground/webservice-client/src/main/java/com/techiekernel/ws/jaxws/document/GetServerDetailResponse.java /home/satish/code-base/code-playground/webservice-client/src/main/java/com/techiekernel/ws/jaxws/document/ObjectFactory.java /home/satish/code-base/code-playground/webservice-client/src/main/java/com/techiekernel/ws/jaxws/document/Server.java /home/satish/code-base/code-playground/webservice-client/src/main/java/com/techiekernel/ws/jaxws/document/package-info.java 

We can also use the bellow maven plugin to import the web service.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxws-maven-plugin</artifactId>
    <version>1.12</version>
    <executions>
     <execution>
      <goals>
       <goal>wsimport</goal>
      </goals>
     </execution>
    </executions>
    <configuration>
     <wsdlUrls>
      <wsdlUrl>http://localhost:8080/webservice-JAX-WS-Web-Metro/foobar?wsdl</wsdlUrl>
      <wsdlUrl>http://localhost:8080/webservice-JAX-WS-Web-Metro/foobarRPC?wsdl</wsdlUrl>
      <wsdlUrl>http://localhost:8080/webservice-JAX-WS-Web-Metro/fileImageServer?wsdl</wsdlUrl>
     </wsdlUrls>

     <sourceDestDir>src/main/java</sourceDestDir>
     <verbose>true</verbose>
    </configuration>
   </plugin>

Client for Document style web service:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package com.techiekernel.ws.jaxws.client;

import com.techiekernel.ws.jaxws.document.FooBarImpl;
import com.techiekernel.ws.jaxws.document.FooBarImplService;

public class DocumentStyleWsClient {
 public static void main(String[] args) {
  FooBarImplService fooBarImplService = new FooBarImplService();
  FooBarImpl fooBarImpl = fooBarImplService.getFooBarImplPort();
  System.out.println(fooBarImpl.callFooBar("Satish"));
 }
}

Client for RPC style web service:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package com.techiekernel.ws.jaxws.client;

import com.techiekernel.ws.jaxws.rpc.FooBarImpl;
import com.techiekernel.ws.jaxws.rpc.FooBarImplService;

public class RPCStyleWsClient {
 public static void main(String[] args) {
  FooBarImplService fooBarImplService = new FooBarImplService();
  FooBarImpl fooBarImpl = fooBarImplService.getFooBarImplPort();
  System.out.println(fooBarImpl.callFooBar1("Satish"));
 }
}


Source Code:

You can pull the source code from GitHub.

1 comment:

  1. hi, i am using your example but when i go to client side the handler doesn't work, i create my web service client in eclipse with new > web service > web service client > put the wsdl and the eclipse generate my web service client, then i put @handlerchain in the service, i start my app but the handler never is being called

    ReplyDelete