July 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Working with Axis2: Making a Java Class into a Service

  • February 7, 2008
  • By Deepal Jayasinghe
  • Send Email »
  • More Articles »

Receiving binary data

public void upload(DataHandler dh) {
   //Logic of saving the file
}

or

public void upload(byte [] byteArray) {
   //Logic of saving the file
}

It is also valid to have binary fields in JavaBeans as well. Axis2 will handle them correctly.

How to Access XML Inside a Service Class

As you know, Axis2 is the native XML support in Axis2, so even POJO Axis2 does support AXIOM. You can write POJO with AXIOM either to send or receive, as well as to write with AXIOM object arrays.

public OMElement echo(OMElement element) {
   return element;
}

Or

public OMElement echo(OMElement [] element) {
   return element;
}

Do You Have Support for Inheritance?

Axis2 POJO supports inheritance, so you can write your service class to extend some other class and publish parent(s) method(s) automatically. In this case, all the public methods of both the parent and child will be published, and of course you can exclude the methods that you do not need to expose, as I discussed earlier.

Say you have a class like this:

package sample;

public class ParentService {
   public String getName() {
      return null;
   }
}

And, say your service class is as follows:

package sample;

public class SampleService extends ParentService {
   public int add(int a, int b) {
      return a + b;
   }
}

Once you deploy the service again, you can see both the parent operation as well as the child operation.

Axis2 has inheritance support for Bean as well, so as an example your Address bean can extend from some other bean. Then, Axis2 will automatically generate WSDL as well. Say you have a bean like this:

package sample;

public class AddressParent {
   private String city;
   private int zipCode;
   public String getCity() {
      return city;
   }
   public void setCity(String city) {
      this.city = city;
   }
   public int getZipCode() {
      return zipCode;
   }

   public void setZipCode(int zipCode) {
      this.zipCode = zipCode;
   }
}

Then you can have your Address bean as follows;

package sample;

public class Address extends AddressParent {
   private String street;
   private String number;

   [Rest of the code goes here]
}

JSR 181 or Annotations Support

Axis2 POJO supports annotations, so you can use any kind of available options in JSR 181. The following is a simple example of a service written using annotations.

package sample;

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

@WebService (name = "MyService" ,serviceName="MyService")
public class SampleService extends ParentService {
   @WebMethod (action = "action:add")
   public int add(int a, int b) {
      return a + b;
   }
}

Conclusion

You have learned about Axis2, creating a Web Service from a Java class, and POJO. Now, it's time to open an IDE and start writing an application of your own.


Tags: Web services



Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel