2012-02-14 68 views
4

我面臨JAXB的以下問題:它看起來像JAXB正在分析從最深的子類到父類的屬性,並且子屬性具有優先級。我想改變這種行爲。特別是:如何告訴JAXB忽略來自父類的屬性

的子類:

package test.sub; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlTransient; 


@XmlAccessorType(XmlAccessType.NONE) 
public class BasicDocument { 

    private String comment; 

    public String getComment() { 
     return comment; 
    } 

    public void setComment(String cost) { 
     this.comment = cost; 
    } 
} 

的父類:

package test; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 

import org.apache.commons.lang.builder.ToStringBuilder; 
import org.apache.commons.lang.builder.ToStringStyle; 

import test.sub.BasicDocument; 

@XmlRootElement(name="Description", namespace="http://www.w3.org/1999/02/22-rdf-syntax-ns#") 
@XmlAccessorType(XmlAccessType.PROPERTY) 
class Document extends BasicDocument { 

    private String identifier; 

    @XmlElement(name = "identifier", namespace = "http://purl.org/dc/terms/") 
    public String getIdentifier() { 
     return identifier; 
    } 

    public void setIdentifier(String identifier) { 
     this.identifier = identifier; 
    } 

    @Override 
    @XmlElement(name = "abstract", namespace = "http://purl.org/dc/terms/") 
    public String getComment() { 
     return super.getComment(); 
    } 

    @Override 
    public String toString() { 
     return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); 
    } 
} 

編組站工作正常:

Document document = new Document(); 

document.setIdentifier("12A"); 
document.setComment("special"); 

StringWriter w = new StringWriter(); 

jaxbContext.createMarshaller().marshal(document, new StreamResult(w)); 

System.out.println(w); 

輸出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<ns2:Description xmlns:ns2="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/dc/terms/"> 
    <abstract>special</abstract> 
    <identifier>12A</identifier> 
</ns2:Description> 

但是編組忽略這是在兒童BasicDocument類的屬性(t.xml正是上面的XML):

JAXBContext jaxbContext = JAXBContext.newInstance(Document.class); 

Document document = (Document) jaxbContext.createUnmarshaller().unmarshal(Document.class.getResourceAsStream("t.xml")); 

System.out.println("out: " + document); 

輸出:

out: Document[identifier=12A,comment=<null>] 

預期:

out: Document[identifier=12A,comment=special] 

基本上@XmlAccessorType(XmlAccessType.NONE)BasicDocument(見Ignore a parent class when Serializing to XML)沒有效果。此外,在包裝test.sub創建package-info.java(見@XmlTransient on third-party or external super class)是這樣的:

@XmlAccessorType(XmlAccessType.NONE) 
package test.sub; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 

沒有效果。只有@XmlTransient public class BasicDocument工作。理想情況下,我不想在孩子身上添加任何註釋並僅通過package-info.java來控制此行爲。我怎樣才能做到這一點?

在JDK 1.6.0_27上測試,另外在classpath中使用JAXB 2.2.4-1運行庫。

它是功能還是bug?

回答

2

您只需將setComment方法添加到Document類。沒有它,即使該方法存在於父類中,它仍被視爲只寫屬性。

public void setComment(String comment) { 
    super.setComment(comment); 
} 

全部來源爲Document

package test; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 

import org.apache.commons.lang.builder.ToStringBuilder; 
import org.apache.commons.lang.builder.ToStringStyle; 

import test.sub.BasicDocument; 

@XmlRootElement(name="Description", namespace="http://www.w3.org/1999/02/22-rdf-syntax-ns#") 
@XmlAccessorType(XmlAccessType.PROPERTY) 
class Document extends BasicDocument { 

    private String identifier; 

    @XmlElement(name = "identifier", namespace = "http://purl.org/dc/terms/") 
    public String getIdentifier() { 
     return identifier; 
    } 

    public void setIdentifier(String identifier) { 
     this.identifier = identifier; 
    } 

    @Override 
    @XmlElement(name = "abstract", namespace = "http://purl.org/dc/terms/") 
    public String getComment() { 
     return super.getComment(); 
    } 

    public void setComment(String comment) { 
     super.setComment(comment); 
    } 

    @Override 
    public String toString() { 
     return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); 
    } 

} 
+0

感謝您的回答。爲什麼'@ XmlTransient'在這種情況下具有魔法治療效果? – 2012-02-15 22:31:03

+1

@dma_k - '@ XmlTransient'在超類上工作,因爲它告訴JAXB實現從繼承層次結構中忽略它,並將屬性視爲屬於子類:http://blog.bdoughan.com/2011 /06/ignoring-inheritance-with-xmltransient.html – 2012-02-16 15:57:24

+0

我面對JAXB的一種「有趣的行爲」。我在'BasicDocument'中引入了一箇中間類'CommentHolder',它包含'String comment'屬性。 ''BasicDocument'增加了'CommentHolder getCommentHolder()'和'setCommentHolder(CommentHolder commentHolder)'代替get/setComment。 'Document'類更新如下:String getComment(){return super.getCommentHolder()。getComment(); }'和'void setComment(String comment){super.getCommentHolder()。setComment(評語); }'所以它的API是一樣的。 JAXB現在序列化'commentHolder'屬性。這是正常的嗎?謝謝 – 2012-02-28 11:14:14