【问题标题】:Truncated Java object when passing through JAX-WS WebService通过 JAX-WS WebService 时截断的 Java 对象
【发布时间】: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


    【解决方案1】:

    如果您的 Web 服务将代理元素定义为传入数据,则不可以将其解组为继承的类。我想可以编写自己的编组器,但这并不像听起来那么容易(我建议不要这样做)。要么为每个类编写一个单独的 WS(混乱),要么使传入的数据具有可以存储其他结构的元素,例如 type:any(也混乱)。事实上,WS 并不完全是 OO。

    【讨论】:

    猜你喜欢
    • 2015-08-26
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    相关资源
    最近更新 更多