Jersey / Jaxb别名豆类列表

时间:2012-08-17 19:50:52

标签: java jaxb jersey

我有一个界面

@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”)添加到接口的方法,实现类的方法和返回参数。我错过了什么吗?感谢

1 个答案:

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