1

我有以下代码正在返回Foo

@GET
@Produces (MediaType.APPLICATION_XML)
public Foo getXML (){
    System.out.println ("getXML Request");
    Foo f = new Foo();
    d.setA("test");
    d.setB("xyxyx");
    return f;
}

我的Foo班级是

@XmlRootElement
public class Foo{

    public void setA(String a) {
        this.a = a;
    }

    public void setB(String b) {
        this.b = b;
    }

        public String getB (){
           return b;
        }

        public String getA (){
           return a;
        }

    @XmlAttribute(name="atrribB")
    private String b;

    @XmlElement(name="elementA")
    private String a;

}

这样做时,我得到了错误FooClass has two properties of the same name "A"对于B.

当我删除getters这两个属性的方法时,一切都很好。我想不创建getter setter并让字段公开吗?

4

1 回答 1

3

您需要注释 get 方法

@XmlRootElement
public class Foo{

    public void setA(String a) {
        this.a = a;
    }

    public void setB(String b) {
        this.b = b;
    }

    @XmlAttribute(name="atrribB")
    public String getB (){
       return b;
    }

    @XmlElement(name="elementA")
    public String getA (){
       return a;
    }

    private String b;

    private String a;

}

或指定@XmlAccessorType(XmlAccessType.FIELD)

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo{

    public void setA(String a) {
        this.a = a;
    }

    public void setB(String b) {
        this.b = b;
    }

    public String getB (){
       return b;
    }

    public String getA (){
       return a;
    }

    @XmlAttribute(name="atrribB")
    private String b;

    @XmlElement(name="elementA")
    private String a;

}

了解更多信息

于 2012-04-27T16:28:43.180 回答