Hello Web Service

We begin by creating the SEI (Service Endpoint Interface) - HelloService. This interface has just one method to take the username as an argument and return a hello message. Note that the method is annotated with @WebMethod. If atleast one method is annotated, then the methods without annotation will not be exposed as web service operation. If none of the methods are annotated, then all the methods of this interface will be exposed as web service operations.

HelloService.java

package com.vinod;

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

@WebService
public interface HelloService {
	
	@WebMethod
	public String sayHello(String user);
	
}

Next, we implement the SEI in the form of DefaultHelloService.java. Note the use of the property serviceName="HelloService" for the annotation @WebService. If this property is not used, then the name of the service will automatically be of pattern {class-name}Service. For example, "DefaultHelloServiceService".

DefaultHelloService.java

package com.vinod;

import javax.jws.WebService;

@WebService(endpointInterface = "com.vinod.HelloService", serviceName="HelloService")
public class DefaultHelloService implements HelloService {

	@Override
	public String sayHello(String user) {
		return String.format("Hello %s, how are you today?", user);
	}

}

That's it. We are done. Since this is a web service, we need a web server to publish this service. The next Java code is an application that hosts a web server at a custom port 9999 and publishes the above web service with a URL "hellows", which is also known as the service endpoint address. The publish() method of the class javax.xml.ws.Endpoint does exactly the same.

ServicePublisher.java

package com.vinod;

import javax.xml.ws.Endpoint;

public class ServicePublisher {
	public static void main(String[] args) {
		String url = "http://localhost:9999/hellows";
		Endpoint.publish(url, new DefaultHelloService());
		System.out.println("The HelloService is published in the url: " + url);
	}
}

If everything is okay, you can check the url http://localhost:9999/hellows?wsdl which exposes the WSDL document for the published web service. In my computer, the output is like this:

<?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://vinod.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://vinod.com/"
	name="HelloWebService">
	<types>
		<xsd:schema>
			<xsd:import namespace="http://vinod.com/"
				schemaLocation="http://localhost:9999/hellows?xsd=1"></xsd:import>
		</xsd:schema>
	</types>
	<message name="sayHello">
		<part name="parameters" element="tns:sayHello"></part>
	</message>
	<message name="sayHelloResponse">
		<part name="parameters" element="tns:sayHelloResponse"></part>
	</message>
	<portType name="HelloService">
		<operation name="sayHello">
			<input message="tns:sayHello"></input>
			<output message="tns:sayHelloResponse"></output>
		</operation>
	</portType>
	<binding name="DefaultHelloServicePortBinding" type="tns:HelloService">
		<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
			style="document"></soap:binding>
		<operation name="sayHello">
			<soap:operation soapAction=""></soap:operation>
			<input>
				<soap:body use="literal"></soap:body>
			</input>
			<output>
				<soap:body use="literal"></soap:body>
			</output>
		</operation>
	</binding>
	<service name="HelloWebService">
		<port name="DefaultHelloServicePort" binding="tns:DefaultHelloServicePortBinding">
			<soap:address location="http://localhost:9999/hellows"></soap:address>
		</port>
	</service>
</definitions>

Note the entries in line numbers 7 and 39. It is the same name we used in the DefaultHelloService class (serviceName property of @WebService annotation). Also, it is worthwile to watch line number 20, which is same as our SEI, and line number 21, which is same as the @WebMethod method.

The next piece of Java code is a client program to create a reference to a dynamic proxy that encapsulate the web service operations, and call the same.

HelloClient.java

package com.vinod;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class HelloClient {
	public static void main(String[] args) throws Exception {
		URL url = new URL("http://localhost:9999/hellows?WSDL");
		QName name = new QName("http://vinod.com/", "HelloWebService");

		Service service = Service.create(url, name);
		HelloService h = service.getPort(HelloService.class);

		String msg = h.sayHello("Vinod");
		System.out.println(msg);

	}
}

Run the client program and you will see a hello message. If you have problem running the client application, check your JDK version. JDK below 1.6 (update 18) need the clientside artifacts to be generated. At the time of writing this, my JDK is 1.6 (update 23).

Download this Eclipse project (How to import?)



Use the following links to navigate