A Technology Blog About Code Development, Architecture, Operating System, Hardware, Tips and Tutorials for Developers.

Monday, November 19, 2012

Web Service using JAX-WS (Document Style)

9:25:00 PM Posted by Satish , , No comments
Here I will be creating a web service using JAX-WS (Document style). Document type web service had some added advantage over RPC style webservice. Most of the situations Document style web services are used. 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 Document style web service end point by using JAX-WS.
2. Create a Web Service Publisher
3. 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-Document -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
51
52
53
54
55
56
<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/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.techiekernel</groupId>
 <artifactId>webservice-JAX-WS-Document</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>webservice-JAX-WS-Document</name>
 <url>http://maven.apache.org</url>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

 <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.document.FooBarPublisher</mainClass>
      </manifest>
     </archive>
    </configuration>
   </plugin>
  </plugins>
  <finalName>webservice-JAX-WS-Document</finalName>
 </build>
</project>

This example I will demonstrate using a Server Object, which will be the return type for one of the web method. So let's create a class called Server, which we will be using to populate the server details and send that back to the 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
31
32
33
34
35
36
37
package com.techiekernel.ws.jaxws.document;

public class Server {
 private String name;
 private String ip;
 private String mac;
 private String os;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getIp() {
  return ip;
 }
 public void setIp(String ip) {
  this.ip = ip;
 }
 public String getMac() {
  return mac;
 }
 public void setMac(String mac) {
  this.mac = mac;
 }
 public String getOs() {
  return os;
 }
 public void setOs(String os) {
  this.os = os;
 }
 @Override
 public String toString() {
  return "Server [name=" + name + ", ip=" + ip + ", mac=" + mac + ", os="
    + os + ", toString()=" + super.toString() + "]";
 }
}

JAX-WS Web Service End Point:

In JAX-WS development, convert from “RPC style” to “Document style” is very easy, just change the @SOAPBinding style option. Actually, annotated with @SOAPBinding is optional, because the default style is document.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
package com.techiekernel.ws.jaxws.document;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;
/**
 * Service end point interface
 * @author satish
 *
 */
@WebService
//@SOAPBinding(style = Style.DOCUMENT, use=Use.LITERAL)
public interface FooBar {
 @WebMethod
 String callFooBar(String name);
 
 @WebMethod
 Server getServerDetail(String client);
}

Web Service Endpoint Implementation:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.techiekernel.ws.jaxws.document;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService(endpointInterface = "com.techiekernel.ws.jaxws.document.FooBar")
public class FooBarImpl implements FooBar {

 public String callFooBar(String name) {
  // TODO Auto-generated method stub
  return "FooBar called by " + name;
 }

 public Server getServerDetail(String client) {
  // TODO Auto-generated method stub
  Server server = new Server();
  server.setName("Techie Kernel");
  server.setIp("192.168.1.0");
  server.setMac("12-75-61-09-12-22");
  server.setOs("Ubuntu");
  return server;
 }
}

Endpoint Publisher:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
package com.techiekernel.ws.jaxws.document;

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-Document/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-Document/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
31
package com.techiekernel.ws.jaxws.document;

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-Document/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://document.jaxws.ws.techiekernel.com/",
    "FooBarImplService");

  Service service = Service.create(url, qname);

  FooBar foobar = service.getPort(FooBar.class);

  System.out.println(foobar.callFooBar("Techie Kernel"));
  System.out.println(foobar.getServerDetail("Techie Kernel").toString());
 }
}

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-Document.jar

2. Test the web service by running client.

1
java com.techiekernel.ws.jaxws.document.FooBarClient

Output:


1
2
FooBar called by Techie Kernel
Server [name=Techie Kernel, ip=192.168.1.0, mac=12-75-61-09-12-22, os=Ubuntu, toString()=com.techiekernel.ws.jaxws.document.Server@1dac0d9]


Code Base:

You can pull the entire source code from GitHub.

0 comments:

Post a Comment