Building a SOAP web service using the JAX-WS API in RPC Style

To build a JAX-WS-based SOAP web service, to perform the following coding steps:

  1. Define the remote interface.
  2. Implement the remote interface as a remote web service.
  3. Write the Java component (publisher) that would expose the web service.
  4. Write the client program to consume this web service.
  1. Define the remote interface. Let's define a remote interface that defines the methods that act as web service methods:
        //Remote Server Interface
package jaxws.rpc;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
//Service End point Interface
@WebService
@SOAPBinding(style = Style.RPC)
public interface RPCServer{
@WebMethod String getRPCServerName(String name);
}
  1. Implement the preceding remote interface that is defined as a remote implementation of the web service:
        //Remote Server implementation
package jaxws.rpc;

import javax.jws.WebService;
//Service Implementation for the interface
@WebService(endpointInterface = "jaxws.rpc.RPCServer")
public class RPCServerImpl implements RPCServer {

@Override
public String getRPCServerName(String name) {
return " JAX-WS RPC Server is : " + name;
}

}
  1. Write the Java component (publisher) that exposes the web service; the publisher class exposes the previously defined remote implementation component as a web service with WSDL in the defined URL:
        //Web Service publisher
package jaxws.rpc;

import javax.xml.ws.Endpoint;

//End point publisher
public class RPCPublisher {
public static void main(String[] args) {
Endpoint.publish
("http://localhost:7779/jaxws/rpcservice", new
RPCServerImpl());
}

}
  1. Write the client program to consume this web service. Once the aforementioned remote publisher is run and the web service is available at the WSDL URL exposed by the publisher, you can write and run the following RPC client program to contact the web service from the client system:
        package jaxws.rpc;

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

public class RPCClient {

public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:
7779/jaxws/rpcservice? wsdl");

//1st argument is the service URI as per the wsdl
//2nd argument is the service name as per the wsdl
QName qname = new QName("http://jaxwsrpcsoapservice.com/",
"RPCServerImplService");
Service service = Service.create(url, qname);
RPCServer rpcWebService =
service.getPort(RPCServer.class);
System.out.println(rpcWebService.getRPCServerName(
"Packt RPC"));
}

}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset