A Web service is a method of communication between two electronic devices over a network. It allows interoperability between different applications on different platforms, for example, a .NET based application on Windows can communicate with a Java based one on Linux. The communication can be done through a set of XML messages over HTTP protocol.

Throughout this article, I will show you how to create and publish a web service, and consume the web service simple Java program. This article supposes you are familiar with basic Java programming using Eclipse IDE.

There are Java API for XML Web Services (JAX-WS) in the Java SE platform since version 6. The API comprises two main packages

  • javax.xml.ws: contains the core JAX-WS APIs.
  • javax.jws: contains annotation classes that describe web service metadata. Annotations help writing web services based applications easily.

In addition, the Java SE platform provides some useful tools for generating web services artifacts: wgen, wsimport, schemagen and xjc. In this article, you will use the wsimport tool for generating Java classes for web services client.

The Server Part

  • Create new Java project in Eclipse (File –> New Java Project). Name the project as WebServiceServer.
  • Create package: com.wordpress.bayurimba.service
  • Create new empty class in the above package, name it as HelloWeb.java. You should end up with a screen like this:
Create HelloWeb Class

Create HelloWeb Class

We will mark it as web service by adding @WebService annotation to the class and implement a web service method by using @WebMethod annotation. Update the source code of the class like the following:

package com.wordpress.bayurimba.service;

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

@WebService
public class HelloWeb {

	@WebMethod
	public String sayHello(String name){
		return "Hello " + name + "!";
	}

}

Both annotation type are from package javax.jws. Continue reading