JAX-WS Example RPC Style
Creating JAX-WS example is a easy task because it requires no extra configuration settings.
JAX-WS API is inbuilt in JDK, so you don't need to load any extra jar file for it. Let's see a simple example of JAX-WS example in RPC style.
There are created 4 files for hello world JAX-WS example:
- HelloWorld.java
- HelloWorldImpl.java
- Publisher.java
- HelloWorldClient.java
The first 3 files are created for server side and 1 application for client side.
JAX-WS Server Code
File: HelloWorld.java
package com.javatpoint; 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 HelloWorld{ @WebMethod String getHelloWorldAsString(String name); }
File: HelloWorldImpl.java
package com.javatpoint; import javax.jws.WebService; @WebService(endpointInterface = "com.javatpoint.HelloWorld") public class HelloWorldImpl implements HelloWorld{ @Override public String getHelloWorldAsString(String name) { return "Hello World JAX-WS " + name; }
}
File: Publisher.java
package com.javatpoint; import javax.xml.ws.Endpoint; public class HelloWorldPublisher{ public static void main(String[] args) { Endpoint.publish(
"http://localhost:7779/ws/hello", new HelloWorldImpl()); }
}
How to view generated WSDL
After running the publisher code, you can see the generated WSDL file by visiting the URL:
JAX-WS Client Code
File: HelloWorldClient.java
package com.javatpoint;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
public class HelloWorldClient{
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:7779/ws/hello?wsdl");
QName qname = new QName("http://javatpoint.com/", "HelloWorldImplService");
Service service = Service.create(url, qname);
HelloWorld hello = service.getPort(HelloWorld.class);
System.out.println(hello.getHelloWorldAsString("javaforbegnnerstutorialrpc"));
}
}
Output:
Hello World JAX-WS javaforbegnnerstutorial rpc
No comments:
Post a Comment