我正在尝试在我的JAX-WS Web服务中创建通用列表包装器。
这是我服务的数据模型:
@XmlType
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
@XmlElement(required = true)
private int personId;
@XmlElement(required = true)
private String personName;
public int getPersonId() {
return personId;
}
public void setPersonId(int personId) {
this.personId = personId;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
}
@XmlType
@XmlAccessorType(XmlAccessType.FIELD)
public class GenericList <T> {
@XmlAnyElement
private List<T> item;
public GenericList() {
item = new ArrayList<T>();
}
public List<T> getItem() {
return item;
}
public void setItem(List<T> item) {
this.item = item;
}
}
@XmlType
@XmlAccessorType(XmlAccessType.FIELD)
public class Application {
@XmlElement(required = true)
private int applicationId;
@XmlElement(required = true)
private GenericList<Person> personList;
public int getApplicationId() {
return applicationId;
}
public void setApplicationId(int applicationId) {
this.applicationId = applicationId;
}
public GenericList<Person> getPersonList() {
return personList;
}
public void setPersonList(GenericList<Person> personList) {
this.personList = personList;
}
}
这是我的网络服务代码:
@WebService(serviceName = "TestWs")
public class TestWs {
@WebMethod
@WebResult(name = "response")
public Application getApplication(@WebParam(name = "request") int applicationId) {
Application app = new Application();
app.setApplicationId(applicationId);
GenericList<Person> personList = new GenericList<Person>();
Random rand = new Random();
Person p1 = new Person();
p1.setPersonId(rand.nextInt(100));
p1.setPersonName("Random 1");
personList.getItem().add(p1);
Person p2 = new Person();
p2.setPersonId(rand.nextInt(100));
p2.setPersonName("Random 2");
personList.getItem().add(p2);
Person p3 = new Person();
p3.setPersonId(rand.nextInt(100));
p3.setPersonName("Random 3");
personList.getItem().add(p3);
app.setPersonList(personList);
return app;
}
}
当我尝试调用Web服务方法getApplication
时,我收到以下错误:
SEVERE: caught throwable
javax.xml.ws.WebServiceException: javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.SAXException2: class core.bean.Person nor any of its super class is known to this context.
javax.xml.bind.JAXBException: class core.bean.Person nor any of its super class is known to this context.]
at com.sun.xml.ws.message.jaxb.JAXBMessage.writePayloadTo(JAXBMessage.java:412)
at com.sun.xml.ws.message.AbstractMessageImpl.writeTo(AbstractMessageImpl.java:155)
at com.sun.xml.ws.api.message.MessageWrapper.writeTo(MessageWrapper.java:230)
at com.sun.xml.ws.encoding.StreamSOAPCodec.encode(StreamSOAPCodec.java:134)
at com.sun.xml.ws.encoding.SOAPBindingCodec.encode(SOAPBindingCodec.java:242)
at com.sun.xml.ws.transport.http.HttpAdapter.encodePacket(HttpAdapter.java:464)
at com.sun.xml.ws.transport.http.HttpAdapter.access$100(HttpAdapter.java:101)
at com.sun.xml.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:697)
at com.sun.xml.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:266)
at com.sun.xml.ws.transport.http.servlet.ServletAdapter.invokeAsync(ServletAdapter.java:225)
at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doGet(WSServletDelegate.java:161)
at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doPost(WSServletDelegate.java:197)
at com.sun.xml.ws.transport.http.servlet.WSServlet.doPost(WSServlet.java:81)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.SAXException2: class core.bean.Person nor any of its super class is known to this context.
javax.xml.bind.JAXBException: class core.bean.Person nor any of its super class is known to this context.]
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:276)
at com.sun.xml.bind.v2.runtime.BridgeImpl.marshal(BridgeImpl.java:104)
at com.sun.xml.bind.api.Bridge.marshal(Bridge.java:145)
at com.sun.xml.ws.db.glassfish.BridgeWrapper.marshal(BridgeWrapper.java:176)
at com.sun.xml.ws.message.jaxb.JAXBMessage.writePayloadTo(JAXBMessage.java:403)
... 33 more
Caused by: com.sun.istack.SAXException2: class core.bean.Person nor any of its super class is known to this context.
javax.xml.bind.JAXBException: class core.bean.Person nor any of its super class is known to this context.
at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:247)
at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:262)
at com.sun.xml.bind.v2.runtime.property.ArrayReferenceNodeProperty.serializeListBody(ArrayReferenceNodeProperty.java:122)
at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:159)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:361)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696)
at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:158)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:361)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696)
at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:158)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:361)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:271)
... 37 more
Caused by: javax.xml.bind.JAXBException: class core.bean.Person nor any of its super class is known to this context.
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:593)
at com.sun.xml.bind.v2.runtime.property.ArrayReferenceNodeProperty.serializeListBody(ArrayReferenceNodeProperty.java:112)
... 47 more
你能解释一下我做错了什么,以及如何正确创建通用列表包装器?
答案 0 :(得分:0)
<强> @XmlRootElement 强> @XmlAccessorType(XmlAccessType.FIELD) 公共类人{ .....