Hi,  Martin
You example makes perfect sense, but the problem which I am experiencing with 
axis2 is different.
I am going to slightly modify  your example to explain. What I did is I have 
added a new field to the subWorks class named title.
I have created a method  called printWorksInfo which takes superclass(Works) as 
an input parameter. In the main class I am instantiating an instance of 
subWorks and passing it to that method.
In plain java not considering axis2 "else if (works instance of subWorks) " 
condition would be true and I would be able to printout title.

What would happen if class fubar would've been exposed as webservice using 
axis2, with printWorksInfo method,  is that by passing to that method subclass 
subWorks would result on the server side in instance of Superclass(Works) and 
that is my problem.
XML which is send from client to the server would've clearly  identify that 
input parameter to the printWorksInfo method passed from the client  is of 
subWorks type, and it would've contain xml tag for title, but on the server 
side inside printWorksInfo it
will be converted to Superclass(Works) and title value will be lost as it does 
not exists on superclass. See my original post I have described this situation 
with XML examples. In axis1 it works fine and in case subclass is passed to the 
method it is correctly desirealized from XML to java object of subclass 
instance. So the problem might be with the way axis2 deserialises XML.

/*contents of base class Works*/
package test;
 public class Works
 {
   public int intValue=0;
   public int getInt() { return this.intValue; }
 }

/*contents of base class subWorks */
package test;
public class subWorks extends Works
{
   public int intValue=1;
   public String title = "title";
   @Override
   public int getInt() { return this.intValue; }
   public String getTitle() {return this.title}
}

/*driver */
package test;
import test.Works;
import test.subWorks;
public class fubar
{
//now a test driver to determine what happens when a subclass is upcasted
 public static void main(String [] args)
 {

  test.subWorks s  =new test.subWorks();
  printWorkInfo(s);
   } //end main
 public void printWorksInfo(Works works) {
  if (works instanceof Works) {
      system.out.println("superclass");
  } else if (works instance of subWorks) {
      system.out.println("subclass");
        Works s = (subWorks) works;
        System.out.println("Works title: " s.getTitle());

   } // end If
} // end printWorksInfo
} //end fubar class


From: Martin Gainty [mailto:[email protected]]
Sent: 11. november 2010 16:41
To: [email protected]
Subject: RE: [AXIS2]. axis2. xsi:type object mapping does not work

Validimir

take this example

/*contents of base class Works*/
package test;
 public class Works
 {
   public int intValue=0;
   public int getInt() { return this.intValue; }
 }

/*contents of base class subWorks */
package test;
public class subWorks extends Works
{
   public int intValue=1;
   @Override
   public int getInt() { return this.intValue; }
}

/*driver */
package test;
import test.Works;
import test.subWorks;
public class fubar
{
//now a test driver to determine what happens when a subclass is upcasted
 public static void main(String [] args)
 {
  test.Works w     =new test.Works();
  test.subWorks s  =new test.subWorks();
   System.out.println("The original base class Works produces"+w.getInt());
     System.out.println("The subclass subWorks produces "+s.getInt());
 //now some upcasting
     Works upcast=(subWorks)s;
     System.out.println("The upcasted subclass produces"+upcast.getInt());
   } //end main
} //end fubar class

classes>java -classpath .;%CLASSPATH% test.fubar
The original base class Works produces0
The subclass subWorks produces 1
The upcasted subclass produces1

/* the subclass when upcasted does NOT take on the characteristics of the 
parent base class */

Martin Gainty
______________________________________________
Jogi és Bizalmassági kinyilatkoztatás/Verzicht und 
Vertraulichkeitanmerkung/Note de déni et de confidentialité

Ez az üzenet bizalmas.  Ha nem ön az akinek szánva volt, akkor kérjük, hogy 
jelentse azt nekünk vissza. Semmiféle továbbítása vagy másolatának készítése 
nem megengedett.  Ez az üzenet csak ismeret cserét szolgál és semmiféle jogi 
alkalmazhatósága sincs.  Mivel az electronikus üzenetek könnyen 
megváltoztathatóak, ezért minket semmi felelöség nem terhelhet ezen üzenet 
tartalma miatt.

Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger 
sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung 
oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem 
Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. 
Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung 
fuer den Inhalt uebernehmen.

Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le 
destinataire prévu, nous te demandons avec bonté que pour satisfaire informez 
l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est 
interdite. Ce message sert à l'information seulement et n'aura pas n'importe 
quel effet légalement obligatoire. Étant donné que les email peuvent facilement 
être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité 
pour le contenu fourni.





________________________________
From: [email protected]
To: [email protected]
Date: Thu, 11 Nov 2010 15:55:40 +0100
Subject: RE: [AXIS2]. axis2. xsi:type object mapping does not work
Hi,

Yes you are right superclass could not be cast to subclass, but in my example I 
am passing Subclass to the method which takes Superclass as an argument, which 
is fine.

From: Supun Malinga [mailto:[email protected]]
Sent: 11. november 2010 14:58
To: [email protected]
Subject: Re: [AXIS2]. axis2. xsi:type object mapping does not work


On Thu, Nov 11, 2010 at 5:30 PM, Vladimir Duloglo 
<[email protected]<mailto:[email protected]>> wrote:
I am experiencing some problems converting XML requests on the server side, in 
case XML requests contain xsi:type parameter.

The setup is following

Two objects:

ObjectSubClass extends ObjectSuperClass

A webservice with a method: getObject(ObjectSuperClass obj)

In wsdl these objects defined in the following way:

<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified"
targetNamespace="http://mypackage.com/xsd";>
<xs:complexType name="ObjectSuperClass">
<xs:sequence>
<xs:element minOccurs="0" name="id" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ObjectSubClass">
<xs:complexContent>
<xs:extension base="ax21:ObjectSuperClass">
<xs:sequence>
<xs:element minOccurs="0" name="phone" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
>From the client I am calling this method by passing to it subclass - 
>ObjectSubClass which has one extrafield -- phone:

ObjectSubClass obj = new ObjectSubclass();
obj.setId("some id");
obj.setName("some name");
obj.setPhone("some phone");
getObject(obj);

In the request XML object is represented like that:

<obj xsi:type="q1:ObjectSubClass" xmlns:q1="http://mypackage.com/xsd";>
<id>1</id>
<name>test</name>
<q1:phone>123</q1:phone>
</obj>
But on the server side no matter if I am passing instance of ObjectSuperClass 
or instance of ObjectSubClass I am getting always instance of superclass 
ObjectSuperClass, even when xsi:type specifies that object is of subclass 
ObjectSubClasstype. As a result I am not getting values present on 
ObjectSubClass and getting only values defined in superclass ObjectSuperClass.
hi,
may be i'm wrong, i'm still a newbe here. :)

but, didn't the casting of the superclass object  to the subclass object work?

thanks,

Same structure worked fine in axis1 ans xsi:type was respected.

Maybe it is some configuration in axis2.

I would be really thankful for any help.





--
Supun Malinga,

Software Engineer,
WSO2 Inc.
http://wso2.com<http://wso2.com/>
http://wso2.org<http://wso2.org/>
email - [email protected]<mailto:[email protected]>
mobile - 071 56 91 321

Reply via email to