JAX WS Hello World Example

In this tutorial we want to run a JAX-WS example (Endpoint + Client) for beginner of JAX-WS web service give the few minutes to this tutorial and happy learning ; ) .

Prerequisites:

  1. JDK 1.6
  2. Eclipse (or STS) IDE

Developing WebService End Point
1) Open Eclipse (or STS), and create a java project “JAXWSServerHello“.

JAX WS Hello World Example

2) Create WSServiceHello Endpoint Interface:

JAX WS Hello World

WSServiceHello .java

package com.dineshonjava.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

/**
 * @author Dinesh Rajput
 *
 */
@WebService
public interface WSServiceHello {
 
 @WebMethod String sayHello(String name);
 
}

3) Create WSServiceHello Endpoint Implementation class:
WSServiceHello .java

package com.dineshonjava.ws;

import javax.jws.WebService;

/**
 * @author Dinesh Rajput
 *
 */
@WebService(endpointInterface="com.dineshonjava.ws.WSServiceHello")
public class WSServiceHelloImpl implements WSServiceHello {

 @Override
 public String sayHello(String name) {
  return  "Hello, Welcome to JAX-WS World and keep learning!! " + name;
 }

}

4) Create Endpoint Publisher class:
WSHelloPublisher.java

package com.dineshonjava.publisher;

import javax.xml.ws.Endpoint;

import com.dineshonjava.ws.WSServiceHelloImpl;

/**
 * @author Dinesh Rajput
 *
 */
public class WSHelloPublisher {

 public static void main(String[] args) {
  Endpoint.publish("http://localhost:8181/WS/WSServiceHello"
    ,new WSServiceHelloImpl());
 }

}

5) Run above program.Your web service is published.You can check your service wsdl at http://localhost:8181/WS/WSServiceHello?wsdl
WSServiceHello.wsdl

<?xml version="1.0" encoding="UTF-8"?>
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.dineshonjava.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.dineshonjava.com/" name="WSServiceHelloImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://ws.dineshonjava.com/" schemaLocation="http://localhost:8181/WS/WSServiceHello?xsd=1"/>
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"/>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"/>
</message>
<portType name="WSServiceHello">
<operation name="sayHello">
<input message="tns:sayHello"/>
<output message="tns:sayHelloResponse"/>
</operation>
</portType>
<binding name="WSServiceHelloImplPortBinding" type="tns:WSServiceHello">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="sayHello">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="WSServiceHelloImplService">
<port name="WSServiceHelloImplPort" binding="tns:WSServiceHelloImplPortBinding">
<soap:address location="http://localhost:8181/WS/WSServiceHello"/>
</port>
</service>
</definitions>

Developing WebService Client :
1) Open eclipse(or STS) and create a new java project “WSHelloClient”

JAX WS Hello Example

2) As you know we need to generate the client stubs… but how?
open your command line, and enter the wsimport command:

CD %CLIENT_PROJECT_HOME%src
wsimport -keep http://localhost:8181/WS/WSServiceHello?wsdl
JAX WS Example

you will find java classes generated and compiled under src->com->dineshonjava->ws

JAX WS


3) Lets create client class now.
create WSHelloClient.java under src->com.dineshonjava.client

package com.dineshonjava.client;

import com.dineshonjava.ws.WSServiceHello;
import com.dineshonjava.ws.WSServiceHelloImplService;

/**
 * @author Dinesh Rajput
 *
 */
public class WSHelloClient {

 public static void main(String[] args) {
  WSServiceHelloImplService service = new WSServiceHelloImplService();
  WSServiceHello serviceHello = service.getWSServiceHelloImplPort();
  System.out.println("***************Call Started*******************");
  System.out.println(serviceHello.sayHello("Dinesh"));
  System.out.println("***************Call Ended*********************");
 }

}

4) Run above program and you will get following output.

JAX

Congratulation, you have successfully created web service endpoint and client. Now in next post, we will deploy it on Tomcat.

Download Source Code 
SourceCode.zip

References
Wikipedia for Web Service

 

<<Previous <<   || Index ||   >>Next >>