Em Ae
Em Ae

Reputation: 8724

Error producing XML from JAXB (WebService)

I have following code which is returning 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;
}

and my Foo Class is

@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;

}

While doing so, I got error on Foo that Class has two properties of the same name "A" and same goes for B.

When i deleted getters method for both of these properties, everything was fine. am i suppose not to create getter setters and leave fields has public ?

Upvotes: 1

Views: 162

Answers (1)

bdoughan
bdoughan

Reputation: 149047

You need to either annotate the get methods

@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;

}

or specify @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;

}

For More Information

Upvotes: 3

Related Questions