Jersey/Jaxb aliasing a List of beans

蓝咒 提交于 2019-12-08 03:41:33

问题


I have a interface

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

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

}

and a bean

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

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


回答1:


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

}  

response

<itemBeans>
    <items>
        <item>
            <name>first</name>
            <description>second</description>
        </item>
        <item>
            <name>first1</name>
            <description>second1</description>
        </item>
    </items>
</itemBeans>


来源:https://stackoverflow.com/questions/12012299/jersey-jaxb-aliasing-a-list-of-beans

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!