java - Using polymorphic JAX-WS webservice parameters -


i have simple jax-ws webservice:

@webservice public class animalfeedingservice {     @webmethod     public void feed(@webparam(name = "animal") animal animal) {         // whatever     } }  @xmlseealso({ dog.class, cat.class }) public abstract class animal {     private double weight;     private string name;     // getters , setters }  public class dog extends animal {}  public class cat extends animal {} 

i create client , call feed instance of dog.

animal mydog = new dog(); mydog .setname("rambo"); mydog .setweight(15); feedingserviceport.feed(mydog); 

the animal in body of soap call looks this:

<animal>     <name>rambo</name>     <weight>15</weight> </animal> 

and unmarshallexception because animal abstract.

is there way have rambo unmarshalled instance of class dog? alternatives?

as might have guessed, xml parser not able determine exact subtype of animal used when requesting because sees generic <animal> , set of tags common types, hence error. jax-ws implementation use? responsibility of client wrap polymorphic types when sending request. in apache cxf (i checked code against newest 2.3.2 version) soap request body looks this:

<animal xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:type="ns2:dog">     <name>rambo</name>     <weight>15.0</weight> </animal> 

the xsi:type="ns2:dog" crucial here. seems jax-ws client sending incorrect request confuses server. try sending request other client, soapui, see whether server reacts properly.

as said, works fine spring/apache cxf , same code you've provided, extracted java interface make cxf happy:

public interface animalfeedingservice {      @webmethod     void feed(@webparam(name = "animal") animal animal);  }  @webservice @service public class animalfeedingserviceimpl implements animalfeedingservice {     @override     @webmethod     public void feed(@webparam(name = "animal") animal animal) {         // whatever     } } 

...and server/client glue code:

<jaxws:endpoint implementor="#animalfeedingservice" address="/animal"/>  <jaxws:client id="animalfeedingserviceclient"               serviceclass="com.blogspot.nurkiewicz.test.jaxws.animalfeedingservice"               address="http://localhost:8080/test/animal"> </jaxws:client> 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -