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.

Prerequisites

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;

@WebService
@SOAPBinding(style = Style.RPC)
public interface Hello {
	@WebMethod
	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 {

	@Override
	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/",
                "HelloImplService");

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

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

        System.out.println(hello.sayHello("Soumitra"));

    }

}

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"/>
    </xsd:schema></types>
    <message name="sayHello">
        <part name="parameters" element="tns:sayHello"/>
    </message>
    <message name="sayHelloResponse">
        <part name="parameters" element="tns:sayHelloResponse"/>
    </message>
    <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"/>
        </operation>
    </portType>
    <binding name="HelloImplPortBinding" type="tns:Hello">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <operation name="sayHello"><soap:operation soapAction=""/>
            <input>
                <soap:body use="literal"/>
            </input>
            <output>
                <soap:body use="literal"/>
            </output>
        </operation>
    </binding>
    <service name="HelloImplService">
        <port name="HelloImplPort" binding="tns:HelloImplPortBinding">
            <soap:address location="http://localhost:8888/jax-ws-ssl/hello"/>
        </port>
    </service>
</definitions>

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.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

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