Jersey / Jaxb别名bean列表 [英] Jersey/Jaxb aliasing a List of beans

查看:118
本文介绍了Jersey / Jaxb别名bean列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个界面

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

.....

如果我提出请求我的回复是

if I make the request my response is

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

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

All of which is fine except the itemBeans tag. How do I get that renamed to items? I tried adding @XmlElement(name = "items") both to the method of the interface, the method of the implementation class, and the return parameter. Am I missing something? Thanks

推荐答案

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

这篇关于Jersey / Jaxb别名bean列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆