JAX-WS

JAX-WS Tutorial

Developing WebService End Point

1) Open Eclipse, and create a java project "WS-Server".
2) Create WS-Service Endpoint Interface:

package mopuri.lakshmikar;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface Greeting {
  @WebMethod String sayHello(String name); }

3) Create WS-Service Endpoint Implementation class:

package mopuri.lakshmikar;
import javax.jws.WebService;
@WebService(endpointInterface = "mopuri.lakshmikar.Greeting")
public class GreetingImpl implements Greeting {
  @Override
   public String sayHello(String name) {
       return "Hello, Welcom to jax-ws " + name;    } }

4) Create Endpoint Publisher class:

package mopuri;
import javax.xml.ws.Endpoint;
import mopuri.lakshmikar.GreetingImpl;
public class WSPublisher {
  public static void main(String[] args) {
        Endpoint.publish("http://localhost:8080/WS/Greeting",new GreetingImpl());   } }

5) Run the WSPublisher…. Guess what .. your WebService is published..
Wow.. check your service wsdl http://localhost:8080/WS/Greeting?wsdl

Developing WebService Client :

1) Open eclipse and create a new java project WS-Client
2) As you know we need to generate the client stubs... but how? 
open your command line, and enter the wsimport command:

1.CD %CLIENT_PROJECT_HOME%\src
2.wsimport –s . http://localhost:8080/WS/Greeting?wsdl

You will find 6 java classes generated, and compiled under src/mopuri/lakshmikar.
You can remove *.class files , no need for them :)

3) Now Lets create Client Class which will be dependent on the stubs:

package mopuri;
import mopuri.lakshmikar.Greeting;
import mopuri.lakshmikar.GreetingImplService;
public class Client {
   public static void main(String[] args){
GreetingImplService service = new GreetingImplService();
Greeting greeting = service.getGreetingImplPort();
System.out.println("------->>  Call Started");
System.out.println(greeting.sayHello("Ali"));
System.out.println("------->>  Call Ended");
  }
}

4) Run the Client Class.... the output should looks like:

----->>  Call Started
Hello, Welcom to jax-ws Ali
----->>  Call Ended


Congratulations.... you managed to develop jax-ws Endpoint , Client.. 

No comments: