0

複数の要素タグを持つ 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 に 1 つのグループを指定すると、上記は正常に機能します。

4

2 に答える 2

1

クラスにルート要素がないと思います。次のように変更します。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Group {
...
...
于 2013-01-07T16:49:40.160 に答える