我试图在我的 java 类中将我的 java 方法注释为 @XmlTransient
,如下所示。
@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
吗?
最佳答案
@XmlTransient
唯一可以使用的方法是以 get
或 set
开头的方法。这些方法组合使用用于公开 Java 中的属性。 @XmlTransient
可以放置在 get
或 set
方法上。
获取方法
get 方法必须不带任何参数并返回一个值:
public String getFoo() {
return foo;
}
设置方法
set 方法必须采用一个参数。
public void setFoo(String foo) {
this.foo = foo;
}
关于java - 在 Jaxb 2.1 中使用 @XmlTransient 注释 java 方法时出现的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20661649/