如何在属性的getter方法上使用@XMLElement重写在类级别指定的JAXB@XMLAccessorType(XMLAccessType.FIELD)?

如何在属性的getter方法上使用@XMLElement重写在类级别指定的JAXB@XMLAccessorType(XMLAccessType.FIELD)?,jaxb,jax-rs,Jaxb,Jax Rs,在下面的示例代码中,Employee类已使用JAXB字段级访问类型指定。但是,对于property dept,访问类型是在getter方法级别使用@XMLElement注释指定的 在编组组织类的过程中,会引发以下异常- com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions Class has two properties of the sa

在下面的示例代码中,Employee类已使用JAXB字段级访问类型指定。但是,对于property dept,访问类型是在getter方法级别使用@XMLElement注释指定的

在编组组织类的过程中,会引发以下异常-

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Class has two properties of the same name "dept"
    this problem is related to the following location:
        at public java.lang.String com.playground.jaxb.Employee.getDept()
    this problem is related to the following location:
        at private java.lang.String com.playground.jaxb.Employee.dept
你能帮我理解为什么JAXB访问器类型的重写不起作用吗?此外,任何解决方案都将受到高度赞赏

示例

根元素类

package com.playground.jaxb;

@XMLRootElement(name="organization")
public class Organization {

    @XmlElementWrapper(name = "employees")
    @XmlElement(name = "employee")
    private Set<Employee> employees;

    public Organization{}

    // Remainder omitted...
}
package com.playground.jaxb;

@XMLAccessorType(XMLAccessType.FIELD)
public class Employee {

    private String name;

    private String dept;

    @XMLElement(name="department")
    public String getDept() {
        return dept;
    }

    public void setDept(String dept) {
        this.dept = dept;
    }

    public Employee {}

    // Remainder omitted...
}

您可以重新命名getter/setter对,例如
getDept()
->
getDepartment()

但在这种情况下,您将在XML中有重复的内容

   <dept>my_dept</dept>
   <department>my_dept</department>

在这种情况下,
dept
字段将被忽略,取而代之的是getter/setter对

Ilya-我更喜欢第二种方法,因为它更干净,并且没有附加任何字符串。然而,我想知道为什么我们需要在字段级别明确提到@xmltransive。如果我们去掉这个显式注释,开发不是很容易吗对JAXB团队有什么建议?!-Thanks@V.Vidyasagar-在Java中,字段及其相应属性之间没有真正的关联。这就是为什么我们没有在您正在寻找的JAXB中添加行为。这里有一些关于JAXB访问类型的附加信息:Blaise——我知道与Groovy等语言不同,Java对字段/相应属性没有太多支持或语言级语义。尽管如此,我觉得这个特性(使@xmltransive成为多余的)如果不是必须具备的话,也是一个很好的开发工具谢谢
   <dept>my_dept</dept>
   <department>my_dept</department>
@XmlTransient
private String dept;

@XmlElement(name="department")
public String getDept() {
    return dept;
}

public void setDept(String dept) {
    this.dept = dept;
}