WebResult与具有多态性的RequestWrapper注释

时间:2011-03-31 17:31:33

标签: jax-ws cxf jaxb2

我有一个如下课程:

@XmlRootElement(namespace = "http://foo.com/model", name = "Person")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "http://foo.com/model", name = "Person"
public abstract Person {
}

两个继承自该类的类:

@XmlRootElement(namespace = "http://foo.com/model", name = "ShortPerson")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "http://foo.com/model", name = "ShortPerson"
public ShortPerson extends Person {
}

@XmlRootElement(namespace = "http://foo.com/model", name = "TallPerson")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "http://foo.com/model", name = "TallPerson"
public TallPerson extends Person {
}

然后我有一个SEI:

@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.WRAPPED, use = SOAPBinding.Use.LITERAL, style = SOAPBinding.Style.DOCUMENT)
@WebService(targetNamespace = "http://foo.com/services")
public interface PersonService {
    @RequestWrapper(localName = "getPersonById", className = "com.foo.services.personService.GetPersonById", targetNamespace = "http://foo.com/services")
    @ResponseWrapper(localName = "getPersonByIdResponse", className = "com.foo.services.personService.GetPersonByUidResponse", targetNamespace = "http://foo.com/services")
    @WebResult(targetNamespace = "http://foo.com/model", name = "Person")
    Person getPersonById(
        @WebParam(targetNamespace = "http://foo.com/services", name = "PersonId") Long personId);

}

我的响应包装类看起来像这样:

@XmlRootElement(name = "getPersonByIdResponse", namespace = "http://foo.com/services")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getPersonByIdResponse", namespace = "http://foo.com/services", propOrder = { "person" })
public class GetPersonByIdResponse {

   @XmlElementRefs( {
        @XmlElementRef(name = "TallPerson", type = TallPerson.class, namespace = "http://foo.com/model"),
        @XmlElementRef(name = "SmallPerson", type = SmallPerson.class, namespace = "http://foo.com/model")

   })
   private Person person;

   public Person getPerson() {
      return person;
   }

   public void setPerson(Person person) {
      this.person = person;
   }

}

所以问题在于,如果我直接通过JAXB测试ResponseWrapper并填充TallPerson对象,我会正确地将该对象编组到特定于TallPerson的XML中(对于SmallPerson也是如此)。

问题是当我在战争中部署它并且服务启用它时,当为TallPerson返回结果时,除了空消息之外,我从Web服务调用中得不到任何内容。实际上,我甚至没有收到< getPersonByIdResponse>的响应包装器根XML元素。

所以我的问题是如果我有一个多态请求或响应对象(在这种情况下是一个响应对象),WebResult名称/目标名称空间如何影响ResponseWrapper中的参数。因为在我的情况下,我正在利用XmlElementRefs将我的基础对象Person正确地封送到TallPerson或SmallPerson中,但这与WebResult上的父类Person的名称空间/名称有什么关系?我不确定冲突是否导致XML响应无法生成。

由于

1 个答案:

答案 0 :(得分:0)

通过使用XmlSeeAlso注释服务,确保所有clases都被绑定到JAXB上下文:

@WebService
@XmlSeeAlso({TallPerson.class,SmallPerson.class})
public interface PersonService {
    @WebResult(name = "Person")
    Person getPersonById(
        @WebParam(name = "PersonId") Long personId);

    }

而且你不需要玩包装纸。 JAX-WS将处理这个问题,它也会生成正确的WSDL。