Published on WSO2 Oxygen Tank (http://wso2.org)

How To Deploy A Simple POJO As A Web Service With WSO2 WSAS

By Suran
Created 2008-01-30 01:32

With WSO2's Web Services Application Server, hassles related to the development, deployment, management and consumption of enterprise level Web services have become a thing of the past. An ever increasing number of users are riding the Web services bandwagon and exposing their services using SOA. But how can a user who may already have some code written in a non-Web services-specific way, perhaps as plain old java objects, make use of the power of Web services? Can it be done without having to rewrite all of the existing code? Thanks to WSO2 WSAS, this is now possible with minimal or zero code change. In this article by Suran Jayathilaka,he describes how this is done.

 

Applies To

WSO2 WSAS v2.2
Sun JDK v1.5

 

For Whom Is This Tutorial?

This tutorial assumes no prior experience with Web services or WSO2 WSAS. In order to complete this tutorial successfully, all you need is a little experience in Java development. And I will presume that you have the Sun JDK 1.5 or higher already installed on your computer.

 

Contents

 

Introduction

Q: How do I deploy my simple Java classes (POJOs) as Web Services?

A: In this tutorial you will see how to expose a simple POJO (Plain Old Java Object) as a Web Service using the WSO2 Web Services Application Server (WSAS). My goal is to help you realize, how easy it is to take any old java class and make it accessible over a network as a Web service, without prior knowledge of Web services development. The creation of Web services has never before been this easy, but thanks to WSO2 WSAS, now it is!

All code used in this tutorial are available for download from here [0].

 

Installing WSO2 WSAS

    (For detailed instructions on how to install WSO2 WSAS on your computer please refer to the Installation Guide [1].)

Deploying a single POJO class

First, we shall see how to expose a single Java class as a Web service using WSO2 WSAS. For this example, I will be considering a simple calculator program. The calculator will provide several mathematical opeartions such as addition, multiplication, division and subtraction. But you should be able to apply the instructions in this tutorial to any POJO that you have or want to write.

One thing you must keep in mind when deploying a single unarchived Java class in WSAS, is that the class cannot be declared in a package other than the default package. You can easily overcome this limitation by packaging your class in a jar and I will elaborate on this later in this tutorial.

Try It

 

Deploying an Annotated jar

If you want your POJO to be deployed in a jar (which is often the case when your POJO consists of several classes), or if you want your service class to be in a package other than the default package, then you need to make use of annotations. Here, by annotations I mean the JSR-181 i.e. Web Services MetaData for Java annotations. Strictly speaking, annotations are only required when there are several classes in the jar, out of which, you need to pick one to be the service - or when you want to selectively expose some of the class' methods as Web service operations. Note that you need to be using Java version 1.5 or higher in order to use annotations.

Invoking the Service

Now that you have exposed your plain old java class as a Web service, let us see how it can be accessed or consumed. In fact, there are several ways in which this can be done, one of which is the Try It tool provided by WSO2 WSAS itself that I talked about briefly earlier. You can also consume your Web service with a simple SOAP (Simple Object Access Protocol) client or in a RESTful (Representational State Transfer) manner.

 

Using WSAS IDE Plugin For Eclipse WTP

By now you should have realized how easy it is to convert plain old Java classes to fully functional Web services using WSO2 WSAS. If you use the Eclipse IDE for Java development, WSAS makes this process even easier thanks to the WSAS IDE plugin for the Eclipse WTP. To learn how the WSAS IDE plugin can be used to develop, debug and deploy Web services right off the IDE in a speedy and convenient manner, refer to this excellent guide by Lahiru Sandakith on Developing Web Services with WSAS IDE [3].

Conclusion

In this tutorial, you learned how to expose a plain old Java object as a Web Service using WSO2 WSAS. You saw how a single .class file can be deployed as well as an annotated class in a jar. You also learned how to consume such services either by writing a client with the help of stubs generated by WSAS or in a RESTful manner.

 

Author

Suran Jayathilaka, Software Engineer at WSO2. suran at wso2 dot com.

 


Source URL:
http://wso2.org/library/3151