Hello World SOAP

Writing Hello World SOAP Service


  • Open your eclipse ide and create a Dynamic Web Project and give it a name HelloworldWS.
  • Under Java Resources source code for the project should be placed with the required package structure. A package called com.example is created and under it create a class called HelloWorldSOAP.java
  • Under WebContent-> WEB-INF create two files web.xml( required for any java web application), sun-jaxws.xml (xml file required to describe the SOAP web services we want to expose)
  • Under WebContent->WEB-INF->lib add the following JAX-WS jars we downloaded  

The directory structure should look like this after setting up


             



Below is the source code for the classes we are going to write
HelloWorldSOAP.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
package com.example;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public class HelloWorldSOAP {
 @WebMethod
 public String sayHello(String name) {
  return "Hello " + name + "!";
 }
}

Web.xml
 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
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
 <display-name>Archetype Created Web Application</display-name>

 <listener>
  <listener-class>
   com.sun.xml.ws.transport.http.servlet.WSServletContextListener
                </listener-class>
 </listener>
 <servlet>
  <servlet-name>hello</servlet-name>
  <servlet-class>
   com.sun.xml.ws.transport.http.servlet.WSServlet
                </servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>hello</servlet-name>
  <url-pattern>/hello</url-pattern>
 </servlet-mapping>

</web-app>

sun-jaxws.xml
1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8"?>
<endpoints
  xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
  version="2.0">
  <endpoint
      name="SOAPTest"
      implementation="com.example.HelloWorldSOAP"
      url-pattern="/hello"/>
</endpoints>


Deploying your first soap service

  • In the eclipse Ide Right Click Server tab -> Add and Remove-> Select HelloWorldSOAP from Available Resources and Add and Finish
  • Right click on Server tab and start
  • Once the server is started without any errors upon hitting the url below you should see the web service wsdl file. This means the service is avaiable now for client consumption
  • http://localhost:8090/HelloWorldWS/hello


Consuming the web service via client

We have the wsdl file describing what services are available and how the client should consume them. Using this as the starting point we can develop client code which can be used to invoke the web service.


  • In eclipse create a new Java project called HelloWorldWSClient. Under src create a new package called com.example.serviceclient. Once created open a command shell and navigate to project directory. In the command shell type the below command
  • wsimport -s src -d bin -p com.example.serviceclient http://localhost:8090/HelloWorldWS/hello?wsdl



The output of the command looks like above where it is generating the client code and putting the source files under <project_dir>/src and class files under <project_dir>/bin. Now you have the client stubs which can be used to call the web service.

Create a class SimpleWSClient. The code for the class should like this


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
package com.example.serviceclient;

/**
 * @author Santosh
 *
 */
public class SimpleWSClient {
 /**
  * This is a Service object providing the client view or the factory for
  * proxies of the Web service from which we generated this. This is used to
  * get the proxy(stub) or port of the web service created.
  */
 HelloWorldSOAPService service = null;

 public SimpleWSClient() {
  /**
   * Instantiate the web service client object.
   */
  service = new HelloWorldSOAPService();
 }

 public void testService(String name) {
  /*
   * Get the web service proxy or port from the web service client
   * factory. This port is nothing but the stub for the web service
   * created. It facilitates us to invoke the web service methods
   * remotely.
   */
  HelloWorldSOAP servicePort = service.getHelloWorldSOAPPort();

  /*
   * Invoke the web service operation using the port or stub or proxy
   */
  String helloMessage = servicePort.sayHello(name);

  System.out.println("Response from web service call : " + helloMessage);
 }

 public static void main(String args[]) {
  SimpleWSClient client = new SimpleWSClient();
  client.testService("inetsolv");
 }

}

Compile this class and run as a java application. The output of the execution should be
       Response from web service call : Hello inetsolv!











No comments:

Post a Comment