creating a java web service

Post on 13-Nov-2014

1.478 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

creating a java web service using eclipse WTP and xml serialization by XStream

TRANSCRIPT

Creating a Java Web Serviceby Haris A.L

1. Introduction

This document explains the steps to create a java web service newly introduced Axis2 Web

Services tools in the Web Tools Platform Project using the latest WTP drivers.

2. Set Up

Tools:

a. IDE – Eclipse Europa Link

b. Web Tools Platform - Link

c. Axis 2 - Link

d. XStream - Link

e. Apache Tomcat Link

3. Creating a Java web service using Axis 2 WTP Tools.

3.1 Install and Configure an Apache Axis2 Web service runtime

In order to create or consume Axis2 Web service components in an Eclipse workspace, the Web

service development tools need to have access to the Axis2 jars to copy them to the Web project

containing the Web service component. Currently, the latest Axis2 version available is Axis2 1.3.

If WTP includes a copy of the Axis2 jars in a plugin, similar to the current implementation of the

tools for Axis 1.3, WTP will need to keep updating the plugin with newer releases of Axis2 as they

become available. Similarly, to the relationship between Server Tools and Apache Tomcat, the

preferred approach is for the user to download the version of Axis2 they want and then point the

Axis2 Web service tools to the location of the download (in zip or unzipped form).

3.1.a Detailed steps

1. In the Preference page, select Web Services -> Axis2 -> Installed Web service runtimes.

2. Expand the Axis2 tree and select Axis2 1.1.

3. Click the Browse button to specify the location of the Axis2 zip or home directory.

4. If the expected Axis2 jars are not found under the specified location, an error message is

shown.

5. Click Finish.

3.1.b Runtime

The above approach is similar to installing a server runtime for Apache Tomcat. The various

Axis2 versions are hard-coded (e.g. Axis2 1.0, 1.1, 1.2, etc.) if the various JARs that are needed

to run Axis2 1.x Web service stays the same, it could be just presented as Axis2 1.x. However, if

down the road when there are more Axis2 versions available and the JARs are different, the Web

service runtime adapter for Axis2 would have to be updated to include the newer Axis2 versions

explicitely.

3.2 Next, we need to create a project with the support of Axis2 features. Open File -> New ->

Other... -> Web -> Dynamic Web Project

3.3 Select the name IndustryWS as the Dynamic Web project name (you can specify any name

you prefer), and select the configured Tomcat runtime as the target runtime.

3.4 Select the Axis2 Web service facet

This will create a dynamic Web project in the workbench.Name the project as

EzPackServicesTest

3.5 After creating classes and required behaviors/functions, we can expose these as

webservices.

Right Click the main service class. Select to create Web service

3.6 The Web service wizard would be brought up with Web service type set to Bottom up Java

bean Web Service with the service implementation automatically filled in. Move the service scale

to Start service. Click on the Web Service runtime link to select the Axis2 runtime.

Click next.

3.7 This page is the service.xml selection page. if you have a custom services.xml, you can

include that by clicking the Browse button. For the moment, just leave it at the default.

3.8 Click next to start the server

3.9 After the server gets started we can click Finish to close the wizard.

If the development environment hosts the web service in localhost we can access the list of

webservices using the url -http://localhost:8080/[PrjName]/services/listServices

The service module can be exported as a war file and hosted in the Tomcat server.

In the sample the list services will look like this,

3.10 Click on the TestService Link to see the wsdl for the service.

4.Generating XMLs from POJOs

Applications use XML used for transporting data between multiplatform and multi protocol

layers.XML provide an open standard for data communication. We had a requirement to send

data as XML to Flex client. There are different XML parsers available to parse XML data and

extract data from it. You can manually generate XML using a separate layer of XML generator

classes. There will be cumbersome work of heavy String appends and manipulations for

generating such XML data. So, we used XStream library to transform java objects to the

attribute–value XML data. XStream can convert instances of Plain Old Java Objects (POJOs) to

XML and back again. This tool will serialize the Java object to XML data. Each row in the

relational database table is mapped to a value object. The result set for a query can have such

similar rows of data. The map value objects to nodes of XML data. The objects are serialized and

de-serialized by this method. This is similar to an object relational mapping framework. You can

use this library is for the ease of fulfilling the requirements. By converting java objects to XML, it is

possible for any client side application communicate with the web service module. The XStream

uses the reflection technology in Java extensively. XStream stands out for its ease of use and low

footprint.

Consider a Class - Industry

public class Industry {

private String name = "";public Industry(){}public String getName() {

return name;}public void setName(String name) {

this.name = name;}

}

We have to convert the object of this class to its XML representation

XStream xs = new XStream();xs.alias("Industry", Industry.class);String data = xs.toXML(new Industry());

The java object will be serialized to XML.

To reconstruct an object, purely from the XML:

Industry newInd = (Industry)xstream.fromXML(data);

Thus, XStream will provide an easy way to provide a data-binding framework for java objects.

top related