Friday 25 December 2015

JAVA : Creating a Webservice using JAX-WS in java

Hi,

Today I am going to demonstrate how we can publish and consume a JAX-WS based webservice.

For this I have created a Simple Java Application that contains webservice that can perform simple calculation
  1. First we need to define what resource or class that we are going to publish.
  2. For that we have to create an Interface named Calculator.java on the basis of which we will publish the webservice. The interface is not exactly needed but this help in maintaining and using the web service from other Java clients far easier.
     
    package calcws.inter;
     
    import javax.jws.WebMethod;
    import javax.jws.WebService;
    import javax.jws.soap.SOAPBinding;
    import javax.jws.soap.SOAPBinding.Style;
     
    //Service Endpoint Interface
    @WebService
    @SOAPBinding(style = Style.RPC)
    public interface Calculator {
        @WebMethod
        Integer addTwoNumbers(Integer firstNum, Integer secondNum);
    }
    
  1. So now we need to create a implementation of the Calculator interface.

    package calcws.impl;
    
    import calcws.inter.Calculator;
    
    import javax.jws.WebService;
    //Service Implementation
    @WebService(serviceName = "CalculatorService", // This is the name of the webservice
                endpointInterface = "calcws.inter.Calculator")
    public class CalculatorImpl implements Calculator {
    
        @Override
        public Integer addTwoNumbers(Integer firstNum, Integer secondNum) {
            // TODO Implement this method
            return firstNum + secondNum;
        }
    }
    
  1. So now we have created the implementation class and now need to publish the class and for this i have created a  Class Publisher.java
    package calcws.publ;
    
    import calcws.impl.CalculatorImpl;
    
    import javax.xml.ws.Endpoint;
    
    public class Publisher {
        public Publisher() {
            super();
        }
    
        public static void main(String[] args) {
            // 1 : Url for the webservice
            // 2 : The the implementation class to be Published
            Endpoint.publish("http://localhost:9999/ws/calc", new CalculatorImpl());
        }
    }
    
  1. Now we have to create a class that will consume the published webservice.
     
    package calcws.client;
    
    import java.net.URL;
    
    import calcws.inter.Calculator;
    
    import javax.xml.namespace.QName;
    import javax.xml.ws.Service;
    
    public class Consumer {
        public Consumer() {
            super();
        }
    
        public static void main(String[] args) throws Exception {
            URL url = new URL("http://localhost:9999/ws/calc?wsdl");
            //1st argument service URI, refer to wsdl document above
            //2nd argument is service name, refer to wsdl document above
            QName qname = new QName("http://impl.calcws/", "CalculatorService");
            Service service = Service.create(url, qname);
            Calculator calc = service.getPort(Calculator.class);
            Integer addTwoNumbers = calc.addTwoNumbers(20, 40);
            System.out.println("The sum is  : " + addTwoNumbers);
        }
    }
    
  1. After this first run the Publisher class and  after that Run the Client to call the service.
    Here is the output.


Reference : http://www.mkyong.com/webservices/jax-ws/jax-ws-hello-world-example/
You can download sampleapplication at :  CalcWS

No comments:

Post a Comment