[英]How to exclude property from XML Binding
我正在嘗試使用JAXB封送一個類似於以下內容的類
@javax.xml.bind.annotation.XmlType(propOrder = {"first", "last"})
public class Person
{
private String first;
private String last;
public String getFirst(){
return first;
}
public void setFirst(String first){
this.first = first;
}
public String getLast(){
return last;
}
public void setLast(String last){
this.first = last;
}
public String getName(){
return this.first + this.last;
}
}
當我嘗試使用以下方法獲取JaxbContext時:
JAXBContext.newInstance(Person.class);
我得到以下異常:
com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 182 counts of IllegalAnnotationExceptions
Property name is present but not specified in @XmlType.propOrder
this problem is related to the following location:
at public java.lang.String myPackage.Person.getName()
at myPackage.Person
這與此處發布的問題非常相似:
https://www.java.net//node/702784
解決方案在哪里添加
@XmlTransient
冒犯性的方法。 但是,我無法編輯Java類來更新注釋。
有什么想法可以超越這一點嗎?
我最終使用了一個名為JAXBIntroductions的庫,該庫使我可以在運行時臨時引入注解,從而減輕了該問題,而又不會造成過多的開銷。 感謝所有看過的人
如果您無法編輯它們,為什么不創建一個新的Person子類並重寫該方法以添加@XmlTransient注釋?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.