2013-12-18 30 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 - 是的。对于属性没有注释限制,所以需要使用方法。 –