从JAXB(WebService)生成XML时出错

时间:2012-04-27 16:14:18

标签: java web-services jaxb marshalling

我有以下代码返回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;

}

在执行此操作时,我Foo的{​​{1}}错误Class has two properties of the same name "A"同样错误。

当我删除这两个属性的B方法时,一切都很好。我想不是要创建吸气剂,并且让田野公开吗?

1 个答案:

答案 0 :(得分: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;

}

了解更多信息