在 Jaxb 2.1 中用 @XmlTransient 注释 java 方法时出现的问题

SRy*_*SRy 2 java xml jaxb

我正在尝试@XmlTransient像下面这样在我的 java 类中注释我的 java 方法。

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

    @XmlTransient
    public void addsomething{

   // do something
    }

}
Run Code Online (Sandbox Code Playgroud)

当我尝试通过其他类在我的 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()
Run Code Online (Sandbox Code Playgroud)

,

但是当我看到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
Run Code Online (Sandbox Code Playgroud)

我不能@XmlTransient在方法上使用吗?

bdo*_*han 5

唯一@XmlTransient可以使用的方法是以get或开头的方法set。这些组合使用的方法用于在 Java 中公开属性。 @XmlTransient可以放在getorset方法上。

获取方法

get 方法必须不带参数并返回一个值:

public String getFoo() {
    return foo;
}
Run Code Online (Sandbox Code Playgroud)

设置方法

set 方法必须接受一个参数。

public void setFoo(String foo) {
    this.foo = foo;
}
Run Code Online (Sandbox Code Playgroud)