Tuesday, June 5, 2018

JAX-WS RPC


JAX-WS RPC Style example


JAX- WS RPC style web services use method name and parameters to generate XML structure.

1. Create a Web Service Endpoint Interface

JAXWSRPC.java
package com.devyan;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style=Style.RPC)
public interface JAXWSRPC {
       @WebMethod
       String getHellowWorldAsString(String name);

}


2. Create a Web Service Endpoint Implementation

JAXWSRPCImpl.java
package com.devyan;

import javax.jws.WebService;

@WebService(endpointInterface="com.devyan.JAXWSRPC")

public class JAXWSRPCImpl implements JAXWSRPC{
       @Override
       public String getHellowWorldAsString(String name)
       {
             return "Hello JAXWS RPC"+name;
       }

}



3. Create a Endpoint Publisher.

Publisher.java
package com.devyan;
import javax.xml.ws.Endpoint;

public class Publisher {

       public static void main(String[] args) {
             // TODO Auto-generated method stub
             Endpoint.publish("http://localhost:7779/ws/hello", new JAXWSRPCImpl());
       }

}

After running the publisher code, you can see the generated WSDL file by visiting the URL:


4.Web Service Client
Create a web service client to access your published service.
JAXWSRPCClient.java
package com.devyan;

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class JAXWSRPCClient {
      
       public static void main(String[] args) throws Exception {
             URL url=new URL("http://localhost:7779/ws/hello?wsdl");
            
             QName qname=new QName("http://devyan.com/","JAXWSRPCImplService");
             Service service=Service.create(url, qname);
             JAXWSRPC jax=service.getPort(JAXWSRPC.class);
             System.out.println(jax.getHellowWorldAsString("Happy Leaning"));
                          
       }

}

OUTPUT:
Hello JAXWS RPCHappy Leaning

0 comments:

Post a Comment