Tuna Töre

> IOC Advocate, Coder

How to use @XmlSeeAlso annotation in JAX-WS


@XmlSeeAlso annotation is used to include custom classes (sub classes) inside client stubs (proxies). This annotation will modify the WSDL file and add a complex type automatically. By doing that, clients will have a complete stub classes .

Lets examine the following scenario;

public abstract class Product() {}
public class Phone extends Product() {}
public class Computer extends Product() {}

@WebService
@XmlSeeAlso({packagename.Phone.class, packagename.Computer.class})
public class MainWebService {

@WebMethod(operationName = “getTheProduct”)
public Product getTheProduct()   {

Product product = new Phone();

      //if you use XmlSeeAlso annotation
     //phone class can be created and returned without an error
//otherwise it will throw an exception in client proxies
//and JAX-WS will only create a Product class in client proxy
return product;

}
}
By adding @XmlSeeAlso annotation to the Web Service

All subclasses of the Product  class  (such as Phone and Computer) will be included inside client proxies so that any kind of casting can be done in these proxies. However if you do not include XMLSeeAlso annotation to your WebService, only the Product class will be included by JAXB and this won’t allow developers to use Phone or Computer classes’ fields and methods inside client proxies.

Sample error if you don’t use XMLSeeAlso annotation

Failed to read a response: javax.xml.bind.UnmarshalException – with linked exception: [javax.xml.bind.UnmarshalException: Unable to create an instance of package.Product - with linked exception: [java.lang.InstantiationException]]

 

 

About these ads

04/06/2011 - Posted by | JAX-WS, Web Services | , , ,

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: