给带有多个元素标签的 xml 时,我得到一个 javax.xml.bind.UnmarshalException。我的 JAXB 注释类如下所示。
@XmlRootElement
public class Group {
private String id;
private String name;
@XmlElementWrapper(name="groups")
@XmlElement(name="group")
private ArrayList<Group> grouplist;
public void setGrouplist(ArrayList<Group> grouplist){
this.grouplist=grouplist;
}
public ArrayList<Group> getGrouplist(){
return grouplist;
}
---------------------------
---------------------------
-------------------------
输入的xml会是这样的......
<groups>
<group>
<id>1</id>
<name>asd</name>
<designation>SE</designation>
</group>
</groups>
我的资源是这样的。
@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
public Response addGroup(@Context HttpServletRequest req)throws JAXBException,IOException{
JAXBContext jaxb = JAXBContext.newInstance(Group.class);
Group grps= (Group)jaxb.createUnmarshaller().unmarshal(req.getInputStream());
当我从 req 提供上述 xml 时,我得到以下异常
com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"groups"). Expected elements are <{}group>: unexpected element (uri:"", local:"groups"). Expected elements are <{}group>
当我通过删除组标记和相应的@xmlelementWrapper 数组列表在我的 xml 中给一个组时,上述工作正常。