JAX-WS webservice example

You may also like to read:

I am going to give an example on how to create a jax-ws webservice. I will show you how to publish the service in two approaches. First approach for publishing the service will be just using the java main method. Second approach will be by deploying the service in Tomcat Server 7 in another tutorial.
So for first approach, creating the java project from Eclipse is sufficient but as we will deploy the service into the Tomcat Server later on so it’s recommended to create the Dynamic Web Project.


JDK 1.6
Eclipse 3.6
List of jars
jax-ws webservice example

Download the jar files and put the jar files under WEB-INF/lib directory.

Create the endpoint interface

package com.roytuts.service;

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

@SOAPBinding(style = Style.RPC)
public interface Hello {
	public String sayHello(String name);

Create the implementation class

package com.roytuts.service;

import javax.jws.WebService;

@WebService(endpointInterface = "com.roytuts.service.Hello")
public class HelloImpl implements Hello {

	public String sayHello(String name) {
		return "Hello " + name;


Now we need to publish the service in order to access the service from the client.

The URL format should be http://<host>:<port>/<any path name>/<any service name>, i.e. http://localhost:8888/jax-ws-ssl/hello

WSDL location should be http://<host>:<port>/<any path name>/<any service name>?wsdl, i.e., http://localhost:8888/jax-ws-ssl/hello?wsdl

package in.webtuts.service.test;

import in.webtuts.service.HelloImpl;

import javax.xml.ws.Endpoint;

public class HelloPublisher {

    public static void main(String[] args) {
        Endpoint.publish("http://localhost:8888/jax-ws-ssl/hello", new HelloImpl());


Now we need to test the service, so we create client class for that.

package in.webtuts.service.test;

import in.webtuts.service.Hello;

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class HelloClient {

    public static void main(String[] args) throws Exception {

        URL url = new URL("http://localhost:8888/jax-ws-ssl/hello?wsdl");

        QName qname = new QName("http://service.roytuts.com/",

        Service service = Service.create(url, qname);

        Hello hello = service.getPort(Hello.class);




WSDL file

<definitions targetNamespace="http://service.roytuts.com/" name="HelloImplService"><types><xsd:schema><xsd:import namespace="http://service.roytuts.com/" schemaLocation="http://localhost:8888/jax-ws-ssl/hello?xsd=1"/>
    <message name="sayHello">
        <part name="parameters" element="tns:sayHello"/>
    <message name="sayHelloResponse">
        <part name="parameters" element="tns:sayHelloResponse"/>
    <portType name="Hello">
        <operation name="sayHello">
            <input wsam:Action="http://service.roytuts.com/Hello/sayHelloRequest" message="tns:sayHello"/>
            <output wsam:Action="http://service.roytuts.com/Hello/sayHelloResponse" message="tns:sayHelloResponse"/>
    <binding name="HelloImplPortBinding" type="tns:Hello">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <operation name="sayHello"><soap:operation soapAction=""/>
                <soap:body use="literal"/>
                <soap:body use="literal"/>
    <service name="HelloImplService">
        <port name="HelloImplPort" binding="tns:HelloImplPortBinding">
            <soap:address location="http://localhost:8888/jax-ws-ssl/hello"/>

Now you can use this WSDL file to consume the service from another system. Just generate stub or client java code from the WSDL file and invoke the service. We will see later how to consume webservice.

Thanks for your reading. Please leave a comment if you have any query.

JAX-WS webservice example

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top