【问题标题】:Marshalling polymorphic objects in JAX-WS在 JAX-WS 中编组多态对象
【发布时间】:2010-11-12 22:09:22
【问题描述】:

我正在创建一个 JAX-WS 类型的 web 服务,其中包含返回对象 WebServiceReply 的操作。 WebServiceReply 类本身包含一个 Object 类型的字段。各个操作将使用几种不同的数据类型填充该字段,具体取决于操作。

发布 WSDL(我使用的是 Netbeans 6.7)并让 ASP.NET 应用程序检索和解析 WSDL 很好,但是当我尝试调用操作时,我会收到以下异常:

javax.xml.ws.WebServiceException: javax.xml.bind.MarshalException
 - with linked exception:
[javax.xml.bind.JAXBException: class [LDataObject.Patient; nor any of its super class is known to this context.]

如何标记 DataObject.Patient 类以及 WebServiceReply 类中的注释以使其正常工作?我也无法根据目标类中的注释来确定关于编组的权威资源,所以如果有人能指出我也很好。

WebServiceReply.java

@XmlRootElement(name="WebServiceReply")
public class WebServiceReply {


    private Object returnedObject;
    private String returnedType;
    private String message;
    private String errorMessage;

    .......... // Getters and setters follow

}

DataObject.Patient.java

@XmlRootElement(name="Patient")

public class Patient {

    private int uid;
    private Date versionDateTime;
    private String name;
    private String identityNumber;

    private List<Address> addressList;
    private List<ContactNumber> contactNumberList;
    private List<Appointment> appointmentList;
    private List<Case> caseList;
}

解决方案

(感谢Gregory Mostizky的回答)

我编辑了 WebServiceReply 类,以便所有可能的返回对象都从新类 ReturnValueBase 扩展,并使用 @XmlSeeAlso 将注释添加到 ReturnValueBase。之后 JAXB 工作正常!

尽管如此,我仍在学习 JAX-WS 中的 JAXB 编组,所以如果有人仍然可以发布任何有关这方面的教程,那就太好了。

Gregory:您可能希望在您的答案中添加返回对象需要从 ReturnValueBase 继承的子类。非常感谢你的帮助!我已经为这个问题发疯了这么久!

【问题讨论】:

    标签: java web-services jaxb jax-ws


    【解决方案1】:

    您需要使用 @XmlSeeAlso 以便您的 JAXB 实现现在也包含其他类。

    在你的情况下,它会是这样的:

    @XmlRootElement
    @XmlSeeAlso({Patient.class, ....})
    public class ReturnValueBase {
    }
    

    并且还将returnedObject 属性更改为ReturnValueBase 类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-25
      • 1970-01-01
      • 2012-03-04
      • 2010-11-29
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      相关资源
      最近更新 更多