2

我有一个界面

@Path("basePath")
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_XML)
public interface SomeService {

@GET
@Path("list")
public List<ItemBean> getItems() throws WebApplicationException;

}

和一颗豆子

@XmlRootElement(name = "item")
@XmlAccessorType(XmlAccessType.FIELD)
public class ItemBean {

@XmlElement(name = "name")
private String someName;
@XmlElement(name = "description")
private String desc20Char;

.....

如果我提出请求,我的回应是

<itemBeans>
   <item>
      <name>foo</name>
      <description>bar</description>
   </item>
   ....etc....
</itemBeans>

除了 itemBeans 标记之外,所有这些都很好。如何将其重命名为项目?我尝试将@XmlElement(name = "items") 同时添加到接口的方法、实现类的方法和返回参数中。我错过了什么吗?谢谢

4

1 回答 1

3
@XmlRootElement(name = "itemBeans")
@XmlAccessorType(XmlAccessType.FIELD)
public class ItemBeans 
{
    @XmlElementWrapper(name = "items")
    @XmlElement(name = "item")
    private List<ItemBean> beans;

    public ItemBeans(List<ItemBean> beans) {
        this.beans = beans;
    }

    public ItemBeans() {
    }
}  

WS

@GET
@Path("list")
public ItemBeans getItems() throws WebApplicationException;

}  

回复

<itemBeans>
    <items>
        <item>
            <name>first</name>
            <description>second</description>
        </item>
        <item>
            <name>first1</name>
            <description>second1</description>
        </item>
    </items>
</itemBeans>
于 2012-08-17T21:51:06.733 回答