rahul
rahul

Reputation: 31

getting javax.xml.bind.UnmarshalException in jaxrs

I am getting an javax.xml.bind.UnmarshalException when giving xml with multiple element tags. My JAXB annotated class is like the one below.

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

The input xml will be like this....

<groups>
<group>
<id>1</id>
<name>asd</name>
<designation>SE</designation>
</group>
</groups>

My resource is like this.

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

When I give the above xml from the req I am getting the below exception

    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>

The above is working fine when i give one group in my xml by removing the groups tag and the corresponding @xmlelementWrapper arraylist.

Upvotes: 0

Views: 680

Answers (2)

Karl M. Davis
Karl M. Davis

Reputation: 616

If you're running into this while using Apache CXF's JAX-RS 2.0 client, you may be hitting CXF-5980.

Upvotes: 0

Dave Richardson
Dave Richardson

Reputation: 5005

I think you are missing a root element in your class. Change it to be:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Group {
...
...

Upvotes: 1

Related Questions