在JAX-WS中编组多态对象

pkc*_*iss 12 java web-services jax-ws jaxb

我正在创建一个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.]
Run Code Online (Sandbox Code Playgroud)

如何在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

}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

(感谢Gregory Mostizky的回答)

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

尽管如此,我仍然在学习JAX-WS中的JAXB编组,所以如果有人仍可以发布任何教程,那将会很棒.

Gregory:您可能希望在返回对象需要从ReturnValueBase子类中添加回答.非常感谢你的帮助!这个问题已经持续了很长时间!

Gre*_*zky 15

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

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

@XmlRootElement
@XmlSeeAlso({Patient.class, ....})
public class ReturnValueBase {
}
Run Code Online (Sandbox Code Playgroud)

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