Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

User Tools

Site Tools

Creating a Webservice

Tool Requirements

  • Eclipse IDE
  • Java JDK (also added to the binary path)

How to create and consume a simple Web Service using JAX WS

In this example, we create a SOAP based web service for a simple Java Calculator class with operations ‘add’ and ‘subtract’. We then create a web service client which consumes the web service and displays the result of the invoked web service.

Creating and Publishing the Web Service

  1. Create a Java Project “CalcWS” and create a package “”.
  2. Create a Java class “Calculator” in this package with the following code:
import javax.jws.WebService;
public class Calculator {
    public int add(int a, int b) {
        return (a + b);
    public int sub(int a, int b) {
        return (a - b);
  1. The @WebService annotation at the beginning of the class definition tells the Java interpreter that we intend to publish ALL the methods of this class as a web service. If we want to publish only particular methods then we can use @WebMethod annotation before the method signature.
  2. In order to publish our class and its methods as web service we need to create appropriate stub files or artifacts for web service deployment and invocation. Fortunately Java provides a tool called ‘wsgen’ which generates JAX-WS portable artifacts used in JAX-WS web services.
  3. Open command prompt go to the project folder “CalcWS”.
  4. Now issue the following command:
wsgen -s src -cp bin  -d bin

The –cp option specifies the classpath for our Calculator class which is in “bin” folder, the –d option specifies where to place generated output files which is also the ‘bin’ folder in our case and the “-s src” tells the generator to create the source class in the src-folder.

  1. In this step we want to publish our class as a web service endpoint. For that we use the static publish() method of the class to publish our “Calculator” class as a web service in the specified context root. Create a class ‘CalcEndpointPublisher’ with main method and type the following code:
public class CalcEndpointPublisher {

    public static void main(String[] args) {
                        new Calculator());
  1. Run this class as “Java Application”.
  2. You may not get output in the Console. To check whether our class is published as web service, open a browser and type the URL mentioned in the endpoint with a parameter ?wsdl appended.


  1. When you run the application, the Java SE 6 platform has a small web application server that will publish the web service at the address http://localhost:8080/CalcWS/Calculator while the JVM is running. If you see a large amount of XML that describes the functionality behind the web service, then the deployment is successful.

Creating and Consuming a Web Service Client

  1. Having published the web service, we now create a client which communicates with the service and displays the result. Create a Java project “CalcWSClient”.
  2. Just like ‘wsgen’, JAX-WS also provides a tool called “wsimport” for generating the artifacts required for creating and consuming a web service. “wsimport” takes a wsdl file as input.
  3. From the project folder “CalcWSClient” in command prompt or terminal, issue the following command: “wsimport -s src -d bin http://localhost:8080/CalcWS/Calculator?wsdl”. (Note: Your Web Service needs to be running)
  4. After refreshing your Eclipse workspace you should see the generated files.
  5. Let us now create a Java class with main method to run this client. Create a package “”. In this package create the class “CalcClient” with the following code:
public class CalcClient {
     public static void main(String[] args) {
        int a = 10;
        int b = 12;
        CalculatorService calcService = new CalculatorService();
        Calculator calc = calcService.getCalculatorPort();
        System.out.println(a + " + " + b + " = " + calc.add(a, b));
        System.out.println(a + " - " + b + " = " + calc.sub(a, b));
  1. Run this class as Java Application. You will get the following output in the console:
 10 + 12 = 22
 10 – 12 = -2
  1. Congratulation for consuming your first web service! :)
teaching/lectures/atsc/2013/howtowebservice.txt · Last modified: 2018/05/09 01:59 (external edit)

SEWiki, © 2024