Here I will be creating a JAX-WS RPC style web service. RPC style web service is having lot of draw back as compared to the Document style web service. But to start with, I thought to give a simple example to understand how RPC style web service implemented in java. You can find the difference between RPC and Document style web services here.
For this tutorial I will be using the following tools
1. JDK 7
2. Eclipse Juno
3. Maven2
So let's start with some concept of JAX-WS before getting in to the code.
JAX-WS is bundled with JDK 1.6, which makes Java web service development easier to develop. This tutorial shows you how to do the following tasks:
1. Create a SOAP-based RPC style web service end point by using JAX-WS.
2. Create a Java web service client manually.
Before start coding let's create a java project using the following maven command.
For this tutorial I will be using the following tools
1. JDK 7
2. Eclipse Juno
3. Maven2
So let's start with some concept of JAX-WS before getting in to the code.
JAX-WS is bundled with JDK 1.6, which makes Java web service development easier to develop. This tutorial shows you how to do the following tasks:
1. Create a SOAP-based RPC style web service end point by using JAX-WS.
2. Create a Java web service client manually.
Before start coding let's create a java project using the following maven command.
1 | mvn archetype:generate -DgroupId=com.techiekernel -DartifactId=webservice-JAX-WS-RPC -Dpackagename=com.techiekernel |
After you execute, the project will be get created with pom.xml file. As I am using JDK 7 and annotations, I have to specify the updated maven plugin. And I also specified a configuration for executable jar. So the final pom.xml is shown bellow.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.techiekernel</groupId> <artifactId>webservice-JAX-WS-RPC</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>webservice-JAX-WS</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> <!-- Build an executable JAR --> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.techiekernel.ws.jaxws.rpc.FooBarPublisher</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> <finalName>webservice-JAX-WS-RPC</finalName> </build> </project> |
JAX-WS Web Service End Point:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package com.techiekernel.ws.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 * @author satish * */ @WebService @SOAPBinding(style = Style.RPC) public interface FooBar { @WebMethod String callFooBar(String name); } |
Web Service Endpoint Implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.techiekernel.ws.jaxws.rpc; import javax.jws.WebMethod; import javax.jws.WebService; @WebService(endpointInterface = "com.techiekernel.ws.jaxws.rpc.FooBar") public class FooBarImpl implements FooBar { @WebMethod public String callFooBar(String name) { // TODO Auto-generated method stub return "FooBar called by " + name; } } |
Endpoint Publisher:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.techiekernel.ws.jaxws.rpc; import javax.xml.ws.Endpoint; /** * End point publisher * @author satish * */ public class FooBarPublisher { public static void main(String[] args) { System.out.println("main called"); Endpoint.publish("http://localhost:8080/webservice-JAX-WS/foobar", new FooBarImpl()); } } |
Once you run FooBarPublisher, the web service "foobar" will be ready with localhost and port 8080. You can curl the service at "http://localhost:8080/webservice-JAX-WS-RPC/foobar?wsdl" and can see the xml defination of the service.
Java Web Service Client:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package com.techiekernel.ws.jaxws.rpc; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; public class FooBarClient { public static void main(String[] args) { URL url = null; try { url = new URL("http://localhost:8080/webservice-JAX-WS-RPC/foobar?wsdl"); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 1st argument service URI, refer to wsdl document above // 2nd argument is service name, refer to wsdl document above QName qname = new QName("http://rpc.jaxws.ws.techiekernel.com/", "FooBarImplService"); Service service = Service.create(url, qname); FooBar foobar = service.getPort(FooBar.class); System.out.println(foobar.callFooBar("Techie Kernel")); } } |
So now we are ready to test our webservice. We are going to do that in two steps.
1. Making the service ready by running the following command in target folder.
1 | java -jar webservice-JAX-WS-RPC.jar |
2. Test the web service by running client.
1 | java com.techiekernel.ws.jaxws.rpc.FooBarClient |
Output:
1 | FooBar called by Techie Kernel |
Code Base:
You can pull the entire source code from GitHub.
0 comments:
Post a Comment