【发布时间】:2013-04-17 16:57:25
【问题描述】:
我目前正在从事一个在 Java 中使用 JAX-WS Web 服务的项目。 全局主题是这样的:用户在本地创建一个对象,比如说一个Agent。他调用第一个 webservice 并将其代理传递给 webservice。 Web 服务处理代理(修改其属性:例如生命点),并将其传递给另一个 Web 服务。此调用是从第一个 Web 服务发出的,因此用户在此过程中无事可做。
在一系列网络服务之后,用户检索已修改的代理。
我的项目的目的是设计两个部分:
- 一个框架,它指定了前面描述的行为:Web 服务、代理和迁移过程
- 一个使用我的框架的演示应用程序。主要区别在于添加了一个 GUI 和一个新类 Avatar,它扩展了 Agent。所以迁移过程仍然是“由框架”完成的,使用 Agent 对象。
以下代码显示了我如何调用我的 web 服务、托管我的 Avatar,然后从服务中检索代理的简单示例:
// connection to the server
URL endpoint= new URL("http://SERVER/tomcat/KiwiBidonDynamique/ServiceWebBidonDeDadou?wsdl");
QName serviceName=new QName("http://avatar/","ServeurKiwiBidonService");
Service service = Service.create(endpoint, serviceName);
WebService port = service.getPort(WebService.class);
Avatar myAvatar = new Avatar(1, "Jack the Ripper");
port.hostAgent(myAvatar);
// some process on the service...
Avatar myAvatarTransformed = (Avatar) port.getAgent("AgentNumberOne");
当我这样做时,我在最后一行得到一个异常:
Exception in thread "main" java.lang.ClassCastException: agent.Agent cannot be cast to avatar.Avatar
经过大量的日志阅读,我猜原因是网络服务的工作方式。当被调用时,我在参数中给出的 Avatar 会在我的 JVM 中编组,然后在服务上解组,但服务仅在解组时构造一个代理。这样做,它会截断特定于 Avatar 的数据。然后,当我尝试从服务中检索我的代理时,它无法转换为 Avatar。
有没有办法在作为代理处理服务时保留头像信息? 我可以以某种方式编写自己的编组/解组吗?
非常感谢。
【问题讨论】:
标签: web-services jax-ws marshalling classcastexception