JAX-WS处理可选的可填充值

时间:2014-09-02 14:51:36

标签: java web-services jaxb jax-ws

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

我有一个新要求,某些字段既可选又可以为nillable。对于这样的字段,如果传递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注释的唯一方法吗?

1 个答案:

答案 0 :(得分:1)

您需要使用带有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);
    }

}