开发者

WebSerices (JAX-WS) and Inheritance

开发者 https://www.devze.com 2023-03-10 01:32 出处:网络
I would like to return a collection of vehicles, where AbstractVehicle is parent of Car and Motorbyte class.

I would like to return a collection of vehicles, where AbstractVehicle is parent of Car and Motorbyte class.

If I have only

   public Collection<AbstractVehicle> getVehicles() {
          Collection<AbstractVehicle> ret = new ArrayList<AbstractVehicle>();
          ret.add(new Car());
          ret.add(new Motorbyte());
          return ret;
   }

I will get result that are only AbstractVehicle.

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getVehiclesResponse xmlns:ns2="http://webservice.delegator.stp.mimos.my/">
<return>
  <weight>0</weight> 
  </return>
<return>
  <weight>0</weight> 
  </return>
  </ns2:getVehiclesResponse>
  </S:Body>
  </S:Envelope>

If I added a dummy method that contain the child classes,

public boolean setAll(Car car, Motorbyte mb) {
              return true;
       }

The getVehicles will return result that are the real child classes.

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"&开发者_StackOverflow中文版gt;
<S:Body>
<ns2:getVehiclesResponse xmlns:ns2="http://webservice.delegator.stp.mimos.my/">
<return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:car">
  <weight>0</weight> 
  <numOfSaftyBelts>0</numOfSaftyBelts> 
  </return>
<return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:motorbyte">
  <weight>0</weight> 
  </return>
  </ns2:getVehiclesResponse>
  </S:Body>
  </S:Envelope>

Why it behaves in this way? Any alternative to get the correct result w/o a dummy method? FYI, I am using Metro http://jax-ws.java.net/. Let me know if you need other info.

UPDATE:

After I added @XmlSeeAlso({Car.class,Motorbyte.class}) on AbstractVehicle class, I am seeing SOAP returns correct type even w/o the dummy method. However, at my client, I got the below error:

Exception in thread "main" com.sun.xml.ws.encoding.soap.DeserializationException: Failed to read a response: javax.xml.bind.UnmarshalException
 - with linked exception:
[javax.xml.bind.UnmarshalException: Unable to create an instance of my.mimos.stp.delegator.webservice.client.AbstractVehicle
 - with linked exception:
[java.lang.InstantiationException]]

So the issue still open. See similar error on other forums but no one provide answer. :(

Thanks.


Apparently this only happens in MyEclipse where it has old version of Metro (1.1). It does not generate @SeeAlso at client stubs, even I specified on AbstractVehicle class. If I build with ant task using JAX-WS Metro 2.1.5 and above, it works w/o issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号