JAX-WS RCP style:
JAX-RPC stands for Java API for XML-based RPC. It allows a Java application to invoke a Java-based Web service with a known description while still being consistent with its WSDL description. JAX-RPC is one of the Java XML programming APIs.
A RPC style SOAP messages body contains an XML representation of the method’s call. It use the method name and its parameters to construct an XML tree which represents the method’s call stack.
Steps:
First create a Web Service Endpoint Interface which will contains the declarations of all the methods we want to include in the Web Service. Create a class which provides the implementation of Endpoint interface. Finally create an Endpoint publisher which deploys the web service and creates and publishes the endpoint at the specified address.
Note: We have to run our Endpoint publisher to make Web Service available to clients. JAX-WS API is inbuilt in JDK.
Web Service:
IHelloWorld.java
import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; /** * Service Endpoint Interface * @author w3schools */ @WebService @SOAPBinding(style = Style.RPC) public interface IHelloWorld { @WebMethod String sayHello(String name); } |
HelloWorld.java
import javax.jws.WebService; /** * Service Endpoint Implementation * @author w3schools */ @WebService(endpointInterface = "com.w3schools.business.IHelloWorld") public class HelloWorld implements IHelloWorld { @Override public String sayHello(String name) { return "JAX-WS RCP Style. Hello " + name; } } |
HelloWorldPublisher.java
import javax.xml.ws.Endpoint; public class HelloWorldPublisher { public static void main(String args[]){ Endpoint.publish("http://localhost:8080/ws/helloWorldRCP", new HelloWorld()); } } |
Web Service Client:
HelloWorldWSClient.java
import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; public class HelloWorldWSClient { public static void main(String args[]){ try { URL url = new URL("http://localhost:8080/ws/helloWorldRCP?wsdl"); QName qname = new QName("http://business.w3schools.com/", "HelloWorldService"); Service service = Service.create(url, qname); IHelloWorld helloWorld = service.getPort(IHelloWorld.class); System.out.println(helloWorld.sayHello("w3schools")); } catch (MalformedURLException e) { e.printStackTrace(); } } } |
How it works:
First, client send a wsdl request to service endpoint (http://localhost:8080/ws/helloWorldRCP?wsdl) and server send the following response.
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1 in JDK 6. --> <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1 in JDK 6. --> <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://business.w3schools.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://business.w3schools.com/"name="HelloWorldService"> <types/> <message name="sayHello"> <part name="arg0" type="xsd:string"/> </message> <message name="sayHelloResponse"> <part name="return" type="xsd:string"/> </message> <portType name="IHelloWorld"> <operation name="sayHello" parameterOrder="arg0"> <input message="tns:sayHello"/> <output message="tns:sayHelloResponse"/> </operation> </portType> <binding name="HelloWorldPortBinding" type="tns:IHelloWorld"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/> <operation name="sayHello"> <soap:operation soapAction=""/> <input> <soap:body use="literal" namespace="http://business.w3schools.com/"/> </input> <output> <soap:body use="literal" namespace="http://business.w3schools.com/"/> </output> </operation> </binding> <service name="HelloWorldService"> <port name="HelloWorldPort" binding="tns:HelloWorldPortBinding"> <soap:address location="http://localhost:8080/ws/helloWorldRCP"/> </port> </service> </definitions> |
A second call, client put method invoke request in SOAP envelope and send it to service endpoint. At the service endpoint, call the requested method and put the result in a SOAP envelope and send it back to client.
Hello w3schools |