Webservices in Java with JAX-WS

Webservice is an application that serves data to other applications in the network. Webservices are independent of programming language that serves or receives data. Main data format is XML and can be transported with regular web protocols, such as HTTP (SOAP).

WSDL (Web Services Description Language) is XML formated description of web service and what is has to offer. It includes abstract definitions of operations and messages as well as protocols and format of messages.

In the following example we will use JAX-WS which is already supported in JDK 1.6.
JAX-WS stands for Java API for XML - Web Services. Java specifies some annotations which simplify implementation of web services (@WebService, @WebMethod, @WebParam, @SOAPBinding...)


First we need to create a class and methods that our webservice will offer. Our service will return a greeting (String), a sum of two numbers (Integer) or an object (Person). A Person is a POJO containing only String name and getter and setter.

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

public class MyService {

public String sayHello(String name) {
return "Hi there " + name;

public Integer add(Integer a, Integer b) {
return a+b;

public Person getPerson(String name) {
Person p = new Person();
return p;


Now we need to publish the service to the web. This class will imitate the server offering a service.

public class WsTest {

public static void main(String[] args) {
MyService ms = new MyService();
Endpoint.publish("", ms);


WSDL document is generated automatically during runtime. To see WSDL open web browser and go to:

Here is my WSDL file (click me).

Create web service client with wsimport

OK, so far we created a service and published it on the web (keep WsTest running).

As implied at the beginning, the WSDL file contains all abstract definitions of operations in web service. Based on these definitions we know exactly what to send to the service and what we can expect as response.

JDK includes a tool called wsimport. It is used to read WSDL file and generate Java classes which we will use to make a client application.

wsimport -p my.project.webservices.generated -keep http://localhost:8080/myservice?wsdl


  • -p defines package names of created classes
  • -d defines directory where classes will be created (here omitted; use current dir)
  • -keep generated .java files as well as .class files
  • the last argument is URL of WSDL

Include the generated classes into web service client project. Implement the WsClient:

public class WsClient {

public static void main(String... a) {

MyServiceService mss = new MyServiceService(); MyService s = mss.getMyServicePort();
System.out.println(s.add(2, 3));

Person person = s.getPerson("Lucy");



Run the WsClient application. You should see the results:

Congratulations! You just created a web service!

Alternative clients

As alternative to our Java WsClient you can create a client in any other programming language (eg. C#). All you need is WSDL.

soapUI is far the best, easy to use application with nice GUI to test web services (did I just say nice to a Java GUI?). Just import WSDL. Search for it on the web.