How to Consume SOAP Webservice in Java using Eclipse IDE

Consume SOAP Webservice in Java

In this tutorial, we will show you the step by step procedures about, How to create SOAP service and consume SOAP webservice in Java using Eclipse IDE. The following example programs were tested with environment and output has been shared in the post.

This tutorial has two part, One part will explain about how to create SOAP service, and Other one will explain about how to consume SOAP webservice in Java using Eclipse IDE. So now lets begin step by step.

#1 Create and Publish SOAP Service

First, Let us create a simple SOAPful webserivce and Publish it in specific URL. So what kind of service we are going to do? This the simple Greeting Service, which will take the input as user name and It will send the response message such as (Good Morning , Good Evening and etc..) based on the current time.

Eg: Output would be “Good Morning Dinesh Krishnan”. If the user name is given as Dinesh Krishnan in between 12.00 AM to 12.00 PM.

#1.1 Service Project Structure

How to Consume SOAP Webservice in Java using Eclipse IDE

In the above Image you will get the idea about Project Structure in Eclipse IDE.

#1.2 GreetingService.java
package com.dineshkrish.soap;

import java.rmi.RemoteException;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style = Style.RPC)
public interface GreetingService {

	public String getMessage(String name) throws RemoteException ;
}
#1.3 GreetingServiceImpl.java
package com.dineshkrish.soap;

import java.rmi.RemoteException;
import java.util.Calendar;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService(endpointInterface = "com.dineshkrish.soap.GreetingService")
@SOAPBinding(style = Style.RPC)
public class GreetingServiceImpl implements GreetingService {

	@Override
	public String getMessage(String name) throws RemoteException {

		String message = "";

		Calendar c = Calendar.getInstance();
		
		int timeOfDay = c.get(Calendar.HOUR_OF_DAY);

		if (timeOfDay >= 0 && timeOfDay < 12) {
			message = "Good Morning " + name;
		} else if (timeOfDay >= 12 && timeOfDay < 16) {

			message = "Good Afternoon " + name;
		} else if (timeOfDay >= 16 && timeOfDay < 21) {

			message = "Good Evening " + name;
		} else if (timeOfDay >= 21 && timeOfDay < 24) {

			message = "Good Night " + name;
		}

		return message;
	}

}
#1.4 Server.java
package com.dineshkrish.soap;

import javax.xml.ws.Endpoint;

public class Server {

	public static boolean publish() {
		
		GreetingService service = new GreetingServiceImpl();
		
		Endpoint endpoint = Endpoint.publish("http://localhost:6565/greeting", service);
		
		return endpoint.isPublished();
	}
	
	public static void main(String[] args) {
		
		boolean status = Server.publish();
		
		if(status) {
			
			System.out.println("Service is up and running at http://localhost:6565/greeting");
			System.out.println("Contract URL is : http://localhost:6565/greeting?wsdl");
		}
	}
}
Publishing SOAP Service

When you add all the above code into project and run the application. It should show you the output, Which is shown in the below attached screen shot and also you will be notified by server (ie: Service is running at http://localhost:6565/greeting).

Consume SOAP Webservice in Java using Eclipse IDE

How to Check Service is Running ?

If you wants to check the service is up and running. Open any Web browser and type following URL such as http://localhost:6565/greeting?wsdl. If everything is fine you will get the below screen in your browser. Which is called WSDL (Web Service Description Language) file. This is the actually called interface, Which is generally provided by the service provider to consume the SOAPful webservices.

How to check service is running

#2 Consuming SOAP Webservice in Java using Eclipse

In this section, We going to consume the greeting service by developing the client application. All the step by step process are attached below.

Step 1

Before going to do this step, Please make sure that you have created the new Java Project in the eclipse with a project name whatever you want. Once the project is created successfully, Right Click on the Project -> Go to New -> Go to Others -> Go to Web Service -> Select Web Service Client. When you do that you will get the following pop up window then Click Next.

Consume SOAP Webservice in Java using Eclipse IDE

Step 2

Followed by step 1 you will get below pop up as next window, Where you have to provide the WSDL file URL such as http://localhost:6565/greeting?wsdl. In order to import source code from the service URL provided.

Consume SOAP Webservice in Java using Eclipse IDE

What you will see?

When above two steps are done successfully, Then you can see that you are import source code(Interface) from the service URL. We are also attached the reference screen shot of project structure below.

How to Consume SOAP Webservice in Java using Eclipse IDE

#2.1 Developing Client for SOAP Service

How to Consume SOAP Webservice in Java using Eclipse IDE

Once all steps are done properly, In order to call service you have to develop client program in your application. The following client program will give you the idea about how to call SOAPful services.

package com.dineshkrish.client;

import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

import com.dineshkrish.soap.GreetingService;
import com.dineshkrish.soap.GreetingServiceImplServiceLocator;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class ServiceClient {

	public static void main(String[] args) {
		
		GreetingServiceImplServiceLocator locator = new GreetingServiceImplServiceLocator();
		
		try {
			
			GreetingService service = locator.getGreetingServiceImplPort();
			
			String message = service.getMessage("Dinesh Krishnan");
			
			System.out.println(message);
			
		} catch (ServiceException e) {
			
			System.out.println(e.getMessage());
			e.printStackTrace();
			
		} catch (RemoteException e) {
			
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		
		
	}
}
Output

That`s all folks, You can see the final output based on the input that you pass to service. We have attached sample output screen shot below.

Consume SOAP Webservice in Java using Eclipse IDE

References

1. Java SOAP API JavaDocs
2. Developing Simple SOAP WebService

Hello, folks, I am a founder of dineshkrish.com. I love Java and Open source technologies, If you find my tutorials are useful, please consider making donations to these charities.