Sunday, October 26, 2014

JAX-WS: HelloWorld Example

Here I am going to give the detailed steps about creating an HelloWorld JAX-Webservice without any IDE.

Steps:

1. Create the following folder structure tomcat's webapps folder

tomcat/webapps

HelloWorldWS
___META-INF
______context.xml
___WEB-INF
______src
_________HelloWorld.java
______classes
______lib
_________activation-1.1.jar
_________webservices-api-2.1-b16.jar
_________webservices-extra-2.1-b16.jar
_________webservices-extra-api-2.1-b16.jar
_________webservices-rt-2.1-b16.jar
_________webservices-tools-2.1-b16.jar
______sun-jaxws.xml
______web.xml

2. HelloWorld.java:

package com.wservice.provider;

import javax.jws.WebService;

@WebService
public class HelloWorld {
public String greetMe(String name) {
return "Hello, Mr. " + name;
}
}

3. Open command prompt and cd to the WEB-INF/ directory

C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\HelloWorldWs\WEB-INF>javac -d classes src\com\wservice\provider\HelloWorld.java

4. context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/HelloWorldWs"/>

5. sun-jaxws.xml

<?xml version="1.0" encoding="UTF-8"?>
<endpoints version="2.0" xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
  <endpoint implementation="com.wservice.provider.HelloWorld" name="HelloWorldServlet" url-pattern="/greet"/>
</endpoints>

6. web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
    <listener>
        <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>HelloWorldServlet</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>HelloWorldServlet</servlet-name>
        <url-pattern>/greet</url-pattern>
    </servlet-mapping>
</web-app>


Thats it on provider side...
Now restart the tomcat server and try accessing the below URL in browser:

http://localhost:8080/HelloWorldWs/greet?wsdl



CONSUMER:

1. Create the following folder structure, for standalone java program

HelloWorldClient
___src
______com.wservice.consumer
__________HelloWorldClientProg.java
___bin

2. Run the following command from HelloWorldClient/ folder

wsimport -s src -d bin -p com.wservice.provider http://localhost:8080/HelloWorldWs/greet?wsdl

This will generate a set of classes.


3. HelloWorldClientProg.java

package com.wservice.consumer;

import com.wservice.provider.HelloWorld;
import com.wservice.provider.HelloWorldService;

public class HelloWorldClientProg {
HelloWorldService service = null;

public HelloWorldClientProg() {
service = new HelloWorldService();
}

public void testService() {
HelloWorld servicePort = service.getHelloWorldPort();

String greetMsg = servicePort.greetMe("Suresh");

System.out.println("Web service call response: " + greetMsg);
}

public static void main(String arags[]) {
HelloWorldClientProg client = new HelloWorldClientProg();
client.testService();
}
}

This should call the respective web service running on tomcat server.

No comments:

Post a Comment