2

I have some service with some WebMethod that returns object of Foo class:

public class Foo {

    private List<Detail> detailList;

    @XmlElement(name = "detail")
    @XmlElementWrapper(name = "detailList")
    public List<Detail> getDetailList() {
        return detailList;
    }

    public void setDetailList(List<Detail> value) {
        this.detailList = value;
    }

    public Foo() {
        this.detailList = new ArrayList();
    }
}

This code produces proper XML like:

<detailList>
    <detail>
        <key></key>
        <value></value>
    </detail>
    <detail>
        <key></key>
        <value></value>
    </detail>
<detailList/>

After building client JAR library it works OK. But I really don't like the code I need to call to get the List:

foo.getDetailList().getDetail();

Because getDetailList() returns DetailList object. How can I have getDetailList() method returning List without any changes in above XML?

chaplean
  • 197
  • 1
  • 3
  • 10
  • Check out: http://stackoverflow.com/questions/2447091/how-generate-xmlelementwrapper-annotation-with-xjc-and-customized-binding – bdoughan Nov 18 '14 at 16:36

1 Answers1

0
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Root", propOrder = {
    "detailList"
})
public class Foo {

    @XmlElementWrapper(name = "detailList", required = true)
    @XmlElement(name = "detail")
    private List<Detail> detailList;

    public List<Detail> getDetailList() {
        return detailList;
    }

    public void setDetailList(List<Detail> value) {
        this.detailList = value;
    }

    public Foo() {
        this.detailList = new ArrayList();
    }
}
aurelius
  • 3,946
  • 7
  • 40
  • 73
  • Thank you for the answer, but changing access type has no effect. wsimport still generates inner class Foo.DetailList with getDetail() method – chaplean Feb 22 '15 at 15:12