我正在使用JAX-WS创建Web服务。它是使用sun-jaxws.xml从@WebService注释类创建的。

我对某些字段同时具有可选和可设置性提出了新要求。对于此类字段,如果传递了nill,我必须写DB NULL,但是如果没有传递值,则我不应该触摸DB值。

为了能够区分这些情况,我尝试将@XmlElementRef添加到此类字段,但是JAXB失败,并显示以下错误:

SEVERE: WSSERVLET11: failed to parse runtime descriptor: javax.xml.ws.WebServiceException: Unable to create JAXBContext
javax.xml.ws.WebServiceException: Unable to create JAXBContext
    at com.sun.xml.ws.model.AbstractSEIModelImpl.createJAXBContext(AbstractSEIModelImpl.java:164)
    at com.sun.xml.ws.model.AbstractSEIModelImpl.postProcess(AbstractSEIModelImpl.java:94)

[...]

Caused by: java.security.PrivilegedActionException: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
There's no ObjectFactory with an @XmlElementDecl for the element {}lastName.
    this problem is related to the following location:
        at private javax.xml.bind.JAXBElement com.example.test.Person.lastName
        at com.example.test.Person
        at public com.example.test.Person com.example.test.jaxws.UpdatePerson.person
        at com.example.test.jaxws.UpdatePerson


题。可以使用JAX-WS完成此任务吗?如果没有,解决此问题的常用方法是什么?

这是我最小的完整示例:

@XmlType(name = "Person")
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
    @XmlElementRef(name="lastName")
    private JAXBElement<String> lastName;

    public JAXBElement<String> getLastName() {
        return lastName;
    }

    public void setLastName(JAXBElement<String> lastName) {
        this.lastName = lastName;
    }

}


我的终点:

@WebService(
  serviceName="CustomTestService",
  portName="DefaultPort",
  targetNamespace="http://ws.example.com/"
)
@SchemaValidation
public class TestEndpoint {

    @WebMethod
    public void updatePerson(
            @WebParam(name="person") Person person
            ) {

        // do nothing
    }

}


编辑:This问题是类似的主题,但我们不使用请求/响应初始模式来提供xjc。使用xjc是使用@XmlElementRef注释的唯一方法吗?

最佳答案

您需要使用用异常描述的ObjectFactory注释的create方法创建@XmlElememtDecl类:

Caused by: java.security.PrivilegedActionException: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
There's no ObjectFactory with an @XmlElementDecl for the element {}lastName.
    this problem is related to the following location:
        at private javax.xml.bind.JAXBElement com.example.test.Person.lastName


它看起来像以下内容:

@XmlRegistry
public class ObjectFactory {

    @XmlElementDecl(name="lastName")
    public JAXBElement<String> createLastName(String value) {
        return new JAXBElement<String>(new QName("lastName"), String.class, value);
    }

}

09-27 02:01
查看更多