JAX-WS:XmlElementWrapper生成额外的客户端代码

时间:2014-11-18 14:59:04

标签: java xml web-services jaxb jax-ws

我有一些服务与一些WebMethod返回Foo类的对象:

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();
    }
}

此代码生成适当的XML,如:

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

构建客户端JAR库后,它可以正常工作。 但我真的不喜欢我需要调用以获取List的代码:

foo.getDetailList().getDetail();

因为getDetailList()返回DetailList对象。 如何让getDetailList()方法返回List 而不对上述XML进行任何更改?

1 个答案:

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