2013-12-18 25 views
1

我想在我的java類中註釋我的java方法爲@XmlTransient,如下所示。在Jaxb 2.1中使用@XmlTransient註釋java方法時的問題2.1

@XmlAccessorType(XmlAccessType.PROPERTY) 
public abstract class MyClass { 

    @XmlTransient 
    public void addsomething{ 

    // do something 
    } 

} 

當我嘗試通過其他類我的JAXBContext來使用這個類我得到以下異常

JAXB annotation is placed on a method that is not a JAXB property 
    this problem is related to the following location: 
     at @javax.xml.bind.annotation.XmlTransient() 

但是,當我看到XmlTransient()註釋定義(@Target(value={FIELD,METHOD,TYPE}))這顯然說是使用方法。而在Javadoc(http://docs.oracle.com/javaee/7/api/javax/xml/bind/annotation/XmlTransient.html)它說

The @XmlTransient annotation can be used with the following program elements: 

a JavaBean property 
field 
class 

我不能使用的方法@XmlTransient

+1

沒有:),因爲Javadoc說你不能這麼做 – WeMakeSoftware

+1

爲什麼你要將'XmlTransient'添加到'void'方法? –

回答

2

@XmlTransient可以使用的唯一方法是以getset開頭的那些方法。這些組合使用的方法用於公開Java中的屬性。 @XmlTransient可以放在getset方法中。

GET方法

get方法必須不帶參數和返回值:

public String getFoo() { 
    return foo; 
} 

設置方法

的一套方法必須採用一個參數。

public void setFoo(String foo) { 
    this.foo = foo; 
} 
+0

那麼在'@ XmlTransient'註釋定義中'METHOD'是指Java Bean屬性的getter或setter? – SRy

+0

@SRy - 是的。對於屬性沒有註釋限制,所以需要使用方法。 –