Thursday, March 26, 2015

JAX-WS Hello World Example

JAX-WS bundle come in JDK 1.6 it help to develop and test JAVA W/S.


Web service is a concpet which helps two different technoloyg to talk and share data with each other without technology barrier.
For Web service implmentation two entity take active part
1- Sender - Publisher
2- Receiver - Client

1- Sender - Publisher :-  This entity is responsible for publishing the W/S to the outer world.
2- Receiver - Client :- This entity is responsible to consume the data published by publisher.


In general words, “endpoint” is a service which is published by Published to the outside  user to access; where “client” access the published service.


1. Create a Web Service Endpoint Interface

package com.test.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

//This class is used as Service Endpoint Interface
@WebService
@SOAPBinding(style = Style.RPC)
public interface HelloWorld{

@WebMethod String getHelloWorld(String name);

}


2. Create a Web Service Endpoint Implementation


package com.test.ws;
import javax.jws.WebService;

//This class is used to implement Service.
@WebService(endpointInterface = "com.test.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld{

@Override
public String getHelloWorld(String name) {
return "Hello World JAX-WS example calling Web Service using Java API for AML using Web Service:" + name;
}


}

3. Create a Endpoint Publisher

package com.test.endpoint;
import javax.xml.ws.Endpoint;
import com.test.ws.HelloWorldImpl;

//this class is used to publish 
public class HelloWorldPublisher{

public static void main(String[] args) {
  Endpoint.publish("http://localhost:9999/ws/hello", new HelloWorldImpl());
    }


}

4. Testing

Once you start the publisher.. our web service will be exposed. To check the same use this url and we will be able to see the WSDL.“http://localhost:9999/ws/hello?wsdl” .



1. Java Web Service Client

package com.test.client;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.test.ws.HelloWorld;

public class HelloWorldClient{

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

URL url = new URL("http://localhost:9999/ws/hello?wsdl");

       
        QName qname = new QName("http://ws.test.com/", "HelloWorldImplService");

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

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

        System.out.println(hello.getHelloWorld("Siddhu"));

    }

}




First execute HelloWorldPublisher and then check publisher is able to expose desired W/S by hitting this WSDL url:-
“http://localhost:9999/ws/hello?wsdl” .

Out Put:-


Hello World JAX-WS example calling Web Service using Java API for AML using Web Service:Siddhu

WSDL Image:- 




No comments: