泽西rest服务显示exceptionjavax.ws.rs.WebApplicationException:javax.xml.bind.MarshalException

我正在研究jersey服务,我在这里提到它在我返回一个java对象时工作正常。 后来我试图让java对象通用它给出exceptionjavax.ws.rs.WebApplicationException:javax.xml.bind.MarshalException

@XmlRootElement public class AppObject implements Serializable { private List list; private String license; public AppObject() { list = new ArrayList(); } public AppObject(List list) { this.list = list; } @XmlAnyElement(lax = true) public List getList() { return list; } public void setList(List list) { this.list = list; } public String getLicense() { return license; } public void setLicense(String license) { this.license = license; } } 

我的服务

 @GET @Produces({MediaType.APPLICATION_XML ,MediaType.APPLICATION_JSON}) @Path("/getreq") public AppObject savePayment() { AppObject appObject = new AppObject(); appObject.setLicense("4"); Long clientKey=4L; List dimreqlist = dimRequirementDao.getAllByClientNIsCurrent(clientKey); appObject.setList(dimreqlist); return appObject; } 

设置为AppObject的DimRequirement

 @XmlRootElement public class DimRequirement extends BaseObject implements Serializable { private Long requirementKey; private String description; private String priority; @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="requirementKey") public Long getRequirementKey() { return this.requirementKey; } public void setRequirementKey(Long requirementKey) { this.requirementKey = requirementKey; } @Column(name="Description") public String getDescription() { return this.description; } public void setDescription(String description) { this.description = description; } @Column(name="Priority") public String getPriority() { return this.priority; } public void setPriority(String priority) { this.priority = priority; } } 

堆栈跟踪

 SEVERE: Mapped exception to response: 500 (Internal Server Error) javax.ws.rs.WebApplicationException: javax.xml.bind.MarshalException - with linked exception: [com.sun.istack.SAXException2: class com.vxl.model.DimRequirement nor any of its super class is known to this context. javax.xml.bind.JAXBException: class com.vxl.model.DimRequirement nor any of its super class is known to this context.] at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:159) at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306) at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1437) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339) 

我引用以下链接链接1 链接2但我无法解决问题。

对于get方法,相应的JAXBContext将基于AppObject类而不是AppObject类型AppObject

 @GET @Produces({MediaType.APPLICATION_XML ,MediaType.APPLICATION_JSON}) @Path("/getreq") public AppObject savePayment() { 

您可以使用@XmlSeeAlso批注使在AppObject类上创建的JAXBContext知道@XmlSeeAlso

 @XmlRootElement @XmlSeeAlso({DimRequirement.class}) public class AppObject implements Serializable {