April 20, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Working with Axis2: Making a Java Class into a Service, Page 2

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

How to Exclude Some of a POJO's Methods

As you know by default, Axis2 exposes all the public methods; howeverv there are instances where you need to exclude or not need to expose all of them. So now, let's look at how to control the behavior. And, say you need to expose only the "echo" and "add" methods and you do not want to expose the "update" method, you can do that by adding the following entry to services.xml. It should be noted here that you cannot exclude operations when you use a one-line deployment mechanism; however, even when you deploy a service programatically, you can exclude operations.

<excludeOperations>
   <operation>update</operation>
</excludeOperations>

Add the above entry to services.xml and redeploy the service. Then, when you click on ?wsdl, you would see only two methods, and you will not see the "update" operation in the WSDL.

What Type of Bean Can You Write?

As discussed above, Axis2 POJO has support for any kind of JavaBeans, but you need to make sure you use the JavaBeans standard when you use JavaBeans as an example:

  • Need to have getter and setter methods.
  • Need to have default constructor.
  • Cannot have properties that start with upper-case letters. As an example, it is not allowed to have a property such as "private String Age" in a bean but you can have "private String age."
  • A bean's properties can be some other bean, primitive types, any kind of object arrays, DataHandlers, and the like.

Now, write a simple JavaBean and try to use that inside the service class. Your bean look like the following.

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

   public String getStreet() {
      return street;
   }

   public void setStreet(String street) {
      this.street = street;
   }

   public String getNumber() {
      return number;
   }

   public void setNumber(String number) {
      this.number = number;
   }
}

Now, you can change your service implementation class and use Address bean as follows;

package sample;

public class SampleService {

   public String echo(String value) {
      return value;
   }

   public int add(int a, int b) {
      return a + b;
   }

   public void update(int c) {

   }
   public Address get(String name) {
      Address address = new Address();
      address.setNumber("Number");
      address.setStreet("Streat");
      return address;
   }
}

Compile the code again, create another service archive file, and redeploy the service. Then, look at the WSDL file carefully; you will see new operations as well as a new schema element for Address in the types section.

Does It Have Support for Object Arrays?

It is possible to write POJO applications with object arrays, and you can have an object array as a field of a bean and as a method in a Service class as shown below. It should be noted here that an object array can be any kind.

public Address[] getAddress(String [] names){

}

Or it is possible to have something like this.

public Address[] getAddress(String [] names ,
   Address[] address ,
   int [] values){
}

How to Write a POJO with Binary Support

You can write your POJO to accept or return Binary data, and there you can use either byte[] or DataHandler. Irrespective of what you use depending on an Axis2 configuration, Axis2 will serialize and de-serialize into Base64 or MTOM. To have binary support, you can write your Service class as shown below.

Sending binary data

public DataHandler getImage(String fileName) {
   File file = new File(fileName);
   DataHandler dh = new DataHandler(new FileDataSource(file));
   return dh;
}

public byte[] getImage(String fileName) {
   //Logic of creating byte array
   return byteArray;
}




Page 2 of 3



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel