adr*_*ser 10 java web-services jax-ws jaxb
我有这个简单的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;
// Also getters and setters
}
public class Dog extends Animal {}
public class Cat extends Animal {}
Run Code Online (Sandbox Code Playgroud)
我创建一个客户端并feed使用一个实例调用Dog.
Animal myDog = new Dog();
myDog .setName("Rambo");
myDog .setWeight(15);
feedingServicePort.feed(myDog);
Run Code Online (Sandbox Code Playgroud)
SOAP调用主体中的动物如下所示:
<animal>
<name>Rambo</name>
<weight>15</weight>
</animal>
Run Code Online (Sandbox Code Playgroud)
我得到一个UnmarshallException因为Animal是抽象的.
有没有办法让Rambo作为一个类的实例解组Dog?我有什么选择?
您可能已经猜到,XML解析器无法确定您在请求时使用的动物的确切子类型,因为它看到的任何东西都是通用的,<animal>并且是一组对所有类型都通用的标记,因此也就是错误.您使用什么JAX-WS实现?客户端有责任在发送请求时正确包装多态类型.在Apache CXF中(我根据最新的2.3.2版本检查了您的代码)SOAP请求主体如下所示:
<animal xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:dog">
<name>Rambo</name>
<weight>15.0</weight>
</animal>
Run Code Online (Sandbox Code Playgroud)
在xsi:type="ns2:dog"这里是至关重要的.看起来你的JAX-WS客户端正在发送混淆服务器的错误请求.尝试与其他客户端(如SoapUI)发送此请求,以查看您的服务器是否正确反应.
正如我所说,它与Spring/Apache CXF完全一致并且与您提供的代码完全相同,我只提取Java接口以使CXF满意:
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
}
}
Run Code Online (Sandbox Code Playgroud)
...和服务器/客户端粘合代码:
<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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8121 次 |
| 最近记录: |