Thursday, May 5, 2016

Starting with: JAX-WS web services, server and client examples.

1) Open Eclipse IDE
2) Create java project named "startingWithJAXWSServer"
3) right-click on src folder and create a new package name com.webservices
4) create into an interface named WebServiceDefinition with this content:
package com.webservices;

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

@WebService
public interface WebServiceDefinition {
 
    @WebMethod
    public String getYourName(String name);
   
}
5) create always in the same package a new class named WebServiceImplementation with this content:

package com.webservices;

import javax.jws.WebService;

@WebService(endpointInterface="com.webservices.WebServiceDefinition")
public class WebServiceImplementation implements WebServiceDefinition {

    public String getYourName (String name) { 
        return "My name is " + name ; 
    }
 
6) Create always in the same class another class named WebServicePublisher, that will be the endpoint publisher, with this content:

package com.webservices;
 
import javax.xml.ws.Endpoint; 
 
public class WebServicePublisher { 
    public static void main(String[] args) { 
        Endpoint.publish("http://localhost:8080/WS/WebServiceDefinition", new WebServiceImplementation() ); 
    } 
}

7) The server is ready. Now we have to run the server(right click on the class or on the project, then "Run As Java Application")
So we have to open a new browser and check under this url:
http://localhost:8080/WS/WebServiceDefinition?wsdl
we'll find the wsdl rapresentation.

Now we can proceed with the client.
8) Create java project named "startingWithJAXWSClient"
9) open a shell and move under $WORKSPACE_HOME_DIR/startingWithJAXWSClient/src
10) give this command: 
wsimport -s . http://localhost:8080/WS/WebServiceDefinition?wsdl
This will generate all stub classes.
(Remember to give F5 on src eclipse folder to refresh its content).
11) Then we have to create a new class named TestWS with this content:
package com.webservices;

public class TestWS {

     
    public static void main(String[] args) { 
             
        WebServiceImplementationService wsServiceImpl = new WebServiceImplementationService (); 
        WebServiceDefinition wsDef = wsServiceImpl.getWebServiceImplementationPort(); 
        System.out.println(wsDef.getYourName("Maurizio"));          
    } 
}

12) Now we can test all running also this class.....

That's all...

Bye...

No comments:

Post a Comment