JAX-WS

JAX-WS stands for Java API for XML Web Services.

Creating a Service Endpoint Interface (SEI)

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


@WebService  
@SOAPBinding(style = Style.DOCUMENT)  
public interface HelloName{  
    @WebMethod String getName(String name);  
}  

Creating a Service Implementation Bean (SIB) that implements all the methods of the SEI.

package com.yairshinar.ws;
 
import javax.jws.WebService;
 
@WebService(endpointInterface = "com.yairshinar.ws.HelloName")
public class HelloNameImplementation implements HelloName{
 
	@Override
	public String getName(String name) {
		return "Hello " + name;
	}
 
}

Create an endpoint publisher and run it:

import javax.xml.ws.Endpoint;

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

}

you can then find a generated  WSDL file at the published URL: http://localhost:7000/ws/hello?wsdl

 

Create a client that connects to the published service and invokes it’s method

package com.yairshinar.ws;
import java.net.URL;  
import javax.xml.namespace.QName;  
import javax.xml.ws.Service;  
public class HelloNameClient{  
    public static void main(String[] args) throws Exception {  
    URL url = new URL("http://localhost:7000/ws/hello?wsdl");  
   
     
        QName qname = new QName("http://com.yairshinar.ws/", "HelloNameImplementationService");  
        Service service = Service.create(url, qname);  
        HelloName hello = service.getPort(HelloName.class);  
        System.out.println(hello.getName("Yair"));  
     }  
 }