2013-10-01 52 views
1

我正在從事球衣服務,我提到here它工作正常,當我返回一個Java對象。後來我試圖使Java對象一般其給予例外 javax.ws.rs.WebApplicationException:javax.xml.bind.MarshalException澤西休息服務顯示異常javax.ws.rs.WebApplicationException:javax.xml.bind.MarshalException

@XmlRootElement 
public class AppObject<T> implements Serializable { 

    private List<T> list; 
    private String license; 

    public AppObject() { 
     list = new ArrayList<T>(); 

    } 

    public AppObject(List<T> list) { 
     this.list = list; 
    } 

    @XmlAnyElement(lax = true) 
    public List<T> getList() { 
     return list; 
    } 

    public void setList(List<T> 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<DimRequirement> savePayment() { 
     AppObject<DimRequirement> appObject = new AppObject<DimRequirement>(); 
     appObject.setLicense("4"); 
     Long clientKey=4L; 
     List<DimRequirement> dimreqlist = dimRequirementDao.getAllByClientNIsCurrent(clientKey); 
     appObject.setList(dimreqlist); 
     return appObject; 

    } 

DimRequirement這是設置爲AppObject

@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) 

我referd以下鏈接 link 1 link 2 ,但我沒能解決問題。

回答

5

對於您的get方法,相應的JAXBContext將構建在類AppObject上,而不是AppObject<DimRequirement>類型。

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

您可以將在AppObject類意識到DimRequirement使用@XmlSeeAlso註解創建JAXBContext

@XmlRootElement 
@XmlSeeAlso({DimRequirement.class}) 
public class AppObject<T> implements Serializable { 
+0

感謝他的工作。我真的想爲每個HTTP GET傳遞不同的對象。我可以添加多個'@XmlSeeAlso({xxxxx.class})@XmlSeeAlso({yyyyy.class})' – jos

+0

@ user2334391 - 您可以執行@XmlSeeAlso({ xxxxx.class,yyyyy.class})'。 –

+0

'{「license」:「12345」,「dimRequirement」:[{「alternateId」:「AlternateID」,「classification」:「1」}]'這是它檢索的json字符串。但是當我解析它給出錯誤**無法識別的字段dimRequirement「(類com.vxl.AppanalytixObject),未標記爲可忽略**' – jos