简体   繁体   English

在JAX-WS中编组多态对象

[英]Marshalling polymorphic objects in JAX-WS

I'm creating a JAX-WS type webservice, with operations that return an object WebServiceReply. 我正在创建一个JAX-WS类型的Web服务,其操作返回一个对象WebServiceReply。 The class WebServiceReply itself contains a field of type Object. WebServiceReply类本身包含Object类型的字段。 The individual operations would populate that field with a few different data-types, depending on the operation. 根据操作,各个操作将使用几种不同的数据类型填充该字段。

Publishing the WSDL (I'm using Netbeans 6.7), and getting a ASP.NET application to retrieve and parse the WSDL was fine, but when I tried to call an operation, I would receive the following exception: 发布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.]

How do I mark the annotations in the DataObject.Patient class, as well as the WebServiceReply class to get it to work? 如何在DataObject.Patient类中标记注释,以及WebServiceReply类以使其工作? I haven't been able to fine a definitive resource on marshalling based upon annotations within the target classes either, so it would be great if anybody could point me to that too. 我无法根据目标类中的注释来编组关于编组的权威资源,所以如果有人能够指出我也会很好。

WebServiceReply.java 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 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;
}

Solution

(Thanks to Gregory Mostizky for his answer) (感谢Gregory Mostizky的回答)

I edited the WebServiceReply class so that all the possible return objects extend from a new class ReturnValueBase, and added the annotations using @XmlSeeAlso to ReturnValueBase. 我编辑了WebServiceReply类,以便所有可能的返回对象都从一个新类ReturnValueBase扩展,并使用@XmlSeeAlso将注释添加到ReturnValueBase。 JAXB worked properly after that! 之后JAXB正常工作!

Nonetheless, I'm still learning about JAXB marshalling in JAX-WS, so it would be great if anyone can still post any tutorial on this. 尽管如此,我仍然在学习JAX-WS中的JAXB编组,所以如果有人仍可以发布任何教程,那将会很棒。

Gregory: you might want to add-on to your answer that the return objects need to sub-class from ReturnValueBase. Gregory:您可能希望在返回对象需要从ReturnValueBase子类中添加回答。 Thanks a lot for your help! 非常感谢你的帮助! I had been going bonkers over this problem for so long! 这个问题已经持续了很长时间!

You need to use @XmlSeeAlso so that your JAXB implementation will now to include additional classes as well. 您需要使用@XmlSeeAlso,以便您的JAXB实现现在也包含其他类。

In your case it would go something like this: 在你的情况下,它将是这样的:

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

And also change returnedObject property to be of type ReturnValueBase. 并且还将returnedObject属性更改为ReturnValueBase类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM