我正在使用 JAX-WS 创建 Web 服务。它是使用 sun-jaxws.xml 从 @WebService
带注释的类创建的。
我对某些字段有一个新要求,既可选又可为空。对于这样的字段,如果传递了 nil,我必须写入 DB NULL,但如果没有传递 value,我不应该触及 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
注释的唯一方法吗?
您需要使用异常描述的带有 @XmlElememtDecl
注释的 create 方法来创建 ObjectFactory
类:
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);
}
}
我是一名优秀的程序员,十分优秀!