我想返回车辆集合,其中 AbstractVehicle 是 Car 和 Motorbyte 类的父级。
如果我只有
public Collection<AbstractVehicle> getVehicles() {
Collection<AbstractVehicle> ret = new ArrayList<AbstractVehicle>();
ret.add(new Car());
ret.add(new Motorbyte());
return ret;
}
我将得到仅 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>
如果我添加了一个包含子类的虚拟方法,
public boolean setAll(Car car, Motorbyte mb) {
return true;
}
getVehicles 将返回真正的子类结果。
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<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>
为什么它会这样?有没有其他方法可以在不使用虚拟方法的情况下获得正确的结果? 仅供引用,我正在使用地铁 http://jax-ws.java.net/ 。如果您需要其他信息,请告诉我。
更新:
在 AbstractVehicle 类上添加 @XmlSeeAlso({Car.class,Motorbyte.class}) 后,我看到 SOAP 即使没有虚拟方法也会返回正确的类型。但是,在我的客户处,我收到以下错误:
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]]
所以这个问题仍然悬而未决。在其他论坛上看到类似的错误,但没有人提供答案。 :(
谢谢。
最佳答案
显然这只发生在 MyEclipse 中,它有旧版本的 Metro (1.1)。即使我在 AbstractVehicle 类上指定,它也不会在客户端 stub 处生成 @SeeAlso 。如果我使用 JAX-WS Metro 2.1.5 及更高版本构建 ant 任务,则它可以正常工作,不会出现问题。
关于java - Web 服务 (JAX-WS) 和继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6229175/