Java Web Service
JAX-WS (Java api for xml web service) : JAX-WS is set of api for creating web services in XML format i.e SOAP(Simple Object Access Protocol). The JAX-WS provides many java annotations for simplify devlopment and deployment of
web service and client.
There are two main commands for creating and deployment webservice and clients ie wsimport and wsgen with many arguments
for details we are giving examples with which you can easily understand the use of wsimport/wsgen commands.
HelloWorld Example (RPC style) : This example show you how to create web service endpoint with RPC style using JAX-WS
web service client with the wsimport command.
JAX-WS comes with jdk1.6 and provides web service development easy.
what is web service endpoint ? i.e web service endpoint is service which published outside for user to access them, and
and web service client is the application who access the published web service.
Steps for creating web service and client
Step 1 : Create web service endpoint interface :
package com.test.ws;
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 sayHello(String name);
}
Step 2 : Create web service endpoint Implementation :
package com.test.ws;
import javax.jws.WebService;
@WebService(endpointInterface = "com.test.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}
Step 3 : Create web service endpoint publisher :
package com.test.endpoint;
import javax.xml.ws.Endpoint;
import com.test.ws.HelloWorldImpl;
//Endpoint publisher
public class TestPublisher{
public static void main(String[] args) {
Endpoint.publish("http://localhost:9999/ws/hello", new HelloWorldImpl());
}
}
Run the web service, your hello world service is deployed in url 'http://localhost:9999/ws/hello'
you can now test using browser as http://localhost:9999/ws/hello?wsdl, after running the wsdl a xml
output comes in the browser for describing the webservice.
Step 4 : Create web service Client:
package com.test.client;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.test.ws.HelloWorld;
public class HelloClient {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://localhost:9999/ws/hello?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://ws.test.com/", "HelloWorldImplService");
Service service = Service.create(url, qname);
HelloWorld hello = service.getPort(HelloWorld.class);
System.out.println(hello.sayHello("Wakil Ahamad"));
}
}
After Running the client the output would be :
Hello Wakil Ahamad
Java Web service client via wsimport tool
The wsimport command parsed the generated wsdl file and create the necessary client (stub) files to accessing
the publised web service.
Issue the following command as
wsimport -verbose -keep http://localhost:9999/ws/hello?wsdl
The above command generates the necessary client files which is provided by wsdl file. It will generate one Interface and one service implementation file
The generated files are :
********HelloWorld.java *******
********HelloWorldImplService.java *********
The Client :
import com.test.ws.HelloWorld;
import com.test.ws.HelloWorldImplService;
public class HelloClient{
public static void main(String[] args) {
HelloWorldImplService helloService = new HelloWorldImplService();
HelloWorld hello = helloService.getHelloWorldImplPort();
System.out.println(hello.getHelloWorldAsString("wakil Ahmad"));
}
}
The output :
Hello Wakil Ahmad
No comments:
Post a Comment