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. The @WebService annotation marks a class as implementing a Web Service, and the @WebMethod annotation is used to exposed a method as web service method.

Now, we create web service class. Create new class in same package as above, name it as Server.java, give a main() method, and then add two line of code to the main() method:

package com.wordpress.bayurimba.service;

import javax.xml.ws.Endpoint;

public class Server {
	public static void main(String[] args) {
		Endpoint.publish("http://localhost:9898/HelloWeb", new HelloWeb());
	    System.out.println("HelloWeb service is ready");
	}
}

The static method publish(String address, Object implementor) creates and publishes a web service at a specified URL. In the code above:

  • A local server is created to listen at the port 9898.
  • A new instance of HelloWeb is created and bind to the URL /HelloWeb

Now, try to run the server as a standalone Java application by selecting Run –> Run As –> Java Application from Eclipse’s main menu. Once the server is started, you should see the following message in console like this:

HelloWeb service is running

HelloWeb service is running

We now try to access the server using browser. Try to access: http://localhost:9898/HelloWeb?wsdl. The server should return an XML document as in the following screenshot.

WSDL Returned from Server

WSDL Returned from Server

This WSDL document describes a web service in XML format so that other applications can discover and consume web service methods.

The Client Part

  • In order to run another Java program (for run the client), open Eclipse IDE again. Don’t use same IDE with the server.
  • Create Java Package, name it as WebServiceClient
  • Open command prompt, change to WebServiceClient project directory. To get WebServiceClient project directory, right click the project, select Properties.
  • In the resource tab, you will find project location
Resource Location

Resource Location

Why we open command prompt? Because we will use wsimport command to generate Java code for Web Service Client based on WSDL. In command prompt, change directory to folder bin. Then execute this command:

wsimport -keep -s ../src/ -p com.wordpress.bayurimba.service.client http://localhost:9898/HelloWeb?wsdl

The result will be like this.

wsimport Execution

wsimport Execution

Now the explanation. As I said above, wsimport is a tool for generating Java classes for web services client. Syntax of the wsimport command line tool is : wsimport [option] <WSDL_URI>

  • <WSDL_URI> : specifies URI of the web service’s WSDL page, it should be http://localhost:9898/HelloWeb?wsdl for our sample program.
  • [options]: specifies some options such as where to read web service source files and where to put generated source files. We will use these three options:
    • -keep: keep the generated files.
    • -s: specifies the directory where to place generated source files.
    • -p: specifies a package to place generated source files.

Now, go back to WebServiceClient in the Eclipse IDE. Refresh the project. You will see generated Java files there. This Java code, are necessary for writing a web service client. We are interested in the class HelloWebService.javabecause it wraps all the details and exposes some methods ready for implementing a client.

Generated Code

Generated Code

Create a new Java class, name it as Client.java under the package com.wordpress.bayurimba.service, with a main() method. Update the source code of the class looks like this:

package com.wordpress.bayurimba.service;

import com.wordpress.bayurimba.service.client.HelloWeb;
import com.wordpress.bayurimba.service.client.HelloWebService;

public class Client {
	public static void main(String[] args) {
		HelloWebService service = new HelloWebService();
		HelloWeb helloWeb = service.getHelloWebPort();
		String response = helloWeb.sayHello("Bayu");
		System.out.println("Web service response: " + response);
	}
}

And we are done writing a web service client. Test it now! Running it by selecting Run –> Run As –> Java Application from Eclipse’s main menu.

 

WebService Client Result

WebService Client Result

Thats all folks!:mrgreen: