Pages Navigation Menu

Coding is much easier than you think

JAX-WS Web services using Eclipse

JAX-WS-Hello-world
 
In this article we shall learn to implement JAX-WS Endpoint and Client using eclipse.
 
Note: In general words, “web service endpoint” is a service which published outside for user to access; where “web service client” is the party who access the published service.
 

Environment Used

 
JDK 7 (Java SE 7)
Eclipse JUNO IDE
 

JAX-WS Web Service End Point

 
Create a java project(File -> New -> Java Project) “JAXWsServer” with the following folder structure.
 
Project Structure
 

WS-Service Endpoint Interface

 
File: Square.java

package com.webServices;

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

@WebService
public interface Square {

	@WebMethod
	public int square(int num);

}

 

WS-Service Endpoint Implementation

 
File: SquareImpl.java

package com.webServices;

import javax.jws.WebService;

@WebService(endpointInterface = "com.webServices.Square")
public class SquareImpl implements Square {

	public int square(int num) {
		return num * num;
	}
}

 

Endpoint publisher

 
File: SquareWSPublisher.java
 

package com.webServices;

import javax.xml.ws.Endpoint;

public class SquareWSPublisher {
	public static void main(String[] args) {
		Endpoint.publish("http://localhost:8091/WS/Square", new SquareImpl());
	}
}

 
Run the endpoint publisher, by right clicking on the above program -> Run As -> Java Application, now your “Square web service” is deployed in URL http://localhost:8091/WS/Square.
 

Test It

 
You can test the deployed web service by accessing the generated WSDL (Web Service Definition Language) document via this URL http://localhost:8091/WS/Square?wsdl.
 

Developing Web Service Client

 
Create a new java project and name it JAXWSClient
 

Client stubs

 
To generate the client stubs you need to open the command prompt and enter the following wsimport command

CD %CLIENT_PROJECT_HOME%src
wsimport - keep http://localhost:8091/WS/Square?wsdl

 
After running the above command, refresh your “JAXWSClient” project in eclipse, Now you will find java classes generated under src folder of “JAXWSClient” project.
 
JAX Client Structure
 
Where is wsimport?
This wsimport tool is bundle with the JDK, you can find it at “JDK_PATH/bin” folder.
 

Client Class

 
Lets create Client Class which consumes the web services.

package com.client;

import com.webservices.Square;
import com.webservices.SquareImplService;

public class Client {

	public static void main(String[] args) {
		SquareImplService service = new SquareImplService();
		Square square = service.getSquareImplPort();
		System.out.println("Square of 10 is- " + square.square(10));
	}
}

 

Run it

Now right click on the above program -> Run As -> Java Application and you will get following output.

Square of 10 is- 100

 
That’s all on how to create a JAX-WS Endpoint and Client in eclipse.
 

About Mohaideen Jamil


Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.