java - Entry cannot be cast to javax.xml.bind.JAXBElement -


i keep getting jaxb cast error.

i'm not sure correct @ point.

the unmarshalling:

try{         client client = client.create();         client.addfilter(new httpbasicauthfilter(api_key, ""));         webresource webresource = client.resource("https://url.entries.xml");         webresource.setproperty("", api_key);         clientresponse response = webresource.accept("application/xml").get(clientresponse.class);          if(response.getstatus() != 200){             throw new runtimeexception("failed : http error code : " + response.getstatus());         }          string output = response.getentity(string.class);         system.out.println("\n============getftocresponse============");         system.out.println(output);           jaxbcontext jaxbcontext = jaxbcontext.newinstance(entries.class);         unmarshaller unmarshaller = jaxbcontext.createunmarshaller();          entries itsentries = (entries)((jaxbelement)unmarshaller.unmarshal(new stringreader(output))).getvalue();       /*  object o = unmarshaller.unmarshal(new stringreader(output));         system.out.println(o.getclass());*/      }catch(exception e){         e.printstacktrace();     } 

here first 16 lines entry.java class, have annotated setters , getters:

@xmlrootelement(name = "entries") @xmlaccessortype(xmlaccesstype.field) public class entry {       private string datecreated;     private string entryid;     private string field1; private string field2; private string field3; private string field4; 

and here entries class, used list of entry objects:

@xmlrootelement(name = "entries") @xmlaccessortype(xmlaccesstype.field) public class entries extends object {      @xmlelement(name="entries")     private list<entry> entrylist = new arraylist<entry>();      @xmlelement(name="entries")     public list<entry> getentrylist() {         return entrylist;     }       public void setentrylist(list<entry> entrylist) {         this.entrylist = entrylist;     }  } 

error message:

entry cannot cast javax.xml.bind.jaxbelement

question update

entries:

@xmlrootelement(name = "entries") @xmlaccessortype(xmlaccesstype.field) public class entries {      @xmlelement(name = "entries")     private list<entry> entrylist = new arraylist<entry>();       public list<entry> getentrylist() {         return this.entrylist;     }       public void setentrylist(list<entry> entrylist) {         this.entrylist = entrylist;     }  } 

entry:

@xmlaccessortype(xmlaccesstype.field) public class entry {

private string datecreated; private string entryid; private string field1; private string field2; private string field3; 

xml:

<?xml version="1.0" encoding="utf-8"?> <entries> <entry> <entryid>1</entryid> <field3>john</field3> <field4>doe</field4> <field12>21 jump street</field12> <field14></field14> <field15>usa</field15> <field11>usa</field11> </entry> </entries> 

as far can see have several issues in current code. second issue listed here root cause of exception.

1. not right class provided jaxbcontext.newinstance(class)

you supposed provide class of root element seems entries in case should jaxbcontext.newinstance(entries.class)

2. way extract entries instance

the expected code rather this:

entries itsentries = (entries)unmarshaller.unmarshal(new stringreader(output)); 

this reason why exception, unmarshal provides directly type corresponding root element not jaxbelement seem expect.

3. mapping not correct

up in class entries, entries mapped twice on field entrylist , on method getentrylist() not correct need map once, since set annotation @xmlaccessortype(xmlaccesstype.field), supposed annotate field.

@xmlrootelement(name = "entries") @xmlaccessortype(xmlaccesstype.field) public class entries {      @xmlelement(name="entries")     private list<entry> entrylist = new arraylist<entry>();      public list<entry> getentrylist() {         return entrylist;     } 

the second issue fact have 2 classes entry , entries mapped exact same element's name using annotation @xmlrootelement(name = "entries") such jaxb cannot know class use when finds root element name entries, in case class entries root element annotation should set on class only. mapping of class entry defined on mapping of field entrylist such have no need set annotation on class.

@xmlaccessortype(xmlaccesstype.field) public class entry {     ... }  @xmlrootelement(name = "entries") @xmlaccessortype(xmlaccesstype.field) public class entries {     ... } 

the third issue related fact don't map elements entry in class entries reason why have empty list when try unmarshal xml content, correct mapping is:

@xmlelement(name="entry") private list<entry> entrylist = new arraylist<entry>(); 

4. no need extend object

assuming referring java.lang.object, have no need make class entries extends java.lang.object implicit.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -