, , ,

In this tutorial we will demonstrate how to create a very simple jax-ws web service.

First let us answer two questions:

1. what is jax-ws

The answer is very easy. You can google it. Below is a quote from wiki

“The Java API for XML Web Services (JAX-WS) is a Java programming language API for creating web services. It is part of the Java EE platform from Sun Microsystems. Like the other Java EE APIs, JAX-WS uses annotations, introduced in Java SE 5, to simplify the development and deployment of web service clients and endpoints. It is part of the Java Web Services Development Pack. JAX-WS can be used in Java SE starting with version 6.”

2.Why we need jax-ws?

web service is xml. Java is object oriented language, so how to fill the gap between xml and java? The answer is jax-rpc(jax-ws is the successor of jax-rpc)

Here is a very simple sample of jax-ws

1. create a class called Calculator. Please pay attn to the @Webservice annotation

package com.foo.ws.calc;

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);


2. create a publisher class

//a publisher class is served as a “server” so that the client is able to call your service

//When you are using wsimport to generate client stub, you must make sure your publisher class is running. Or else a Connection refused:connect error will be raised.

package com.foo.ws.calc.endpoint;

import javax.xml.ws.Endpoint;

import com.theopentutorials.ws.calc.Calculator;

public class CalcEndpointPublisher {

public static void main(String[] args) {

//whatever you wanna call the service address
Endpoint.publish(“http://localhost:8080/CalcWS/Calculator”, new Calculator());



3.generate stubs via wsgen

Generate the stub(you must make sure you have already generated the Calculator.class file under the bin folder):
wsgen -cp bin -d bin com.foo.ws.calc.Calculator

Generate the src from STUB:
wsgen -s src -cp bin -d bin com.foo.ws.calc.Calculator

Please pay attn to what I am saying. It generate the stubs NOT the Calculator.class

The stubs are located under com\theopentutorials\ws\calc\jaxws and they are:





To generate a wsdl file use

wsgen -cp bin -d bin com.foo.ws.calc.Calculator -wsdl

Then a wsdl file and a xsd file will be generated under your current folder.

Now we have the wsdl file.

Try below to make sure it works


4.Generate the src and stub from SWDL:

wsimport -s src -d bin http://localhost:8080/CalcWS/Calculator?wsdl

5. generate a client to test

package com.foo.ws.calc.client;

import com.foo.ws.calc.Calculator;
import com.foo.ws.calc.CalculatorService;

public class CalcClient {

* @param args
public static void main(String[] args) {
int a = 1;
int b = 2;
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));




You may ask what is the CalculatorService? This class was generated in step 4.