问题
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