You might think that following issue is very simple, but I don\'t know what I did wrong here. I feel I added required dependencies. Haven\'t I?
Could anyone please s
Although Issue is very old but still answering. Root cause is that com.sun.xml.bind is obsolete now. org.glassfish.jaxb is latest reference implementation of JAXB API. Using below JAXB RI maven dependency would solve the issue.
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.2.11</version>
</dependency>
As per link: Why has AnnotationReader been removed from JAXB reference implementation?, you need to simply use below maven dependencies:
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
You need to refactor code a bit. Also looks like you've not created same name fields of model class, it should be like below: Person.java
@XmlRootElement(name="Person")
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
@XmlElement
private String first;
@XmlElement
private String last;
@XmlElement
private String age;
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getLast() {
return last;
}
public void setLast(String last) {
this.last = last;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Person [first=" + first + ", last=" + last + ", age=" + age + "]";
}
}
Book.java
@XmlRootElement(name="book")
@XmlAccessorType(XmlAccessType.FIELD)
public class Book {
private List<Person> person = new ArrayList<Person>();
public List<Person> getPerson() {
return person;
}
public void setPerson(List<Person> person) {
this.person = person;
}
}
ReadXMLFileJaxb.java
public class ReadXMLFileJaxb {
public static void main(String[] args) {
File file = new File(ReadXMLFileDOM.class.getClassLoader().getResource("book.xml").getFile());
try {
JAXBContext context = JAXBContext.newInstance(Book.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Book book = (Book) unmarshaller.unmarshal(file);
System.out.println(book.getPerson().size());
for (int i = 0; i < book.getPerson().size(); i++) {
System.out.println("------------");
System.out.println(book.getPerson().get(i).getFirst());
System.out.println(book.getPerson().get(i).getLast());
System.out.println(book.getPerson().get(i).getAge());
}
} catch (JAXBException e) {
System.out.println(e.getMessage());
}
}
}
The below output I see:
3
------------
Kiran
Pai
22
------------
Bill
Gates
46
------------
Steve
Jobs
40
The best solution that I found is using maven-jaxb2-plugin, then the only dependency that you need is:
<!-- https://mvnrepository.com/artifact/org.jvnet.jaxb2.maven2/maven-jaxb2-plugin -->
<dependency>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.14.0</version>
</dependency>
You seem to be running it inside the IDE. For some strange reasons, although the JAXB classes are included in JRE's rt.jar,
➜ lib jar tvf rt.jar| grep AnnotationReader
4199 Fri Jan 29 15:35:18 PST 2016 com/sun/xml/internal/bind/v2/model/annotation/AbstractInlineAnnotationReaderImpl.class
3140 Fri Jan 29 15:35:14 PST 2016 com/sun/xml/internal/bind/v2/model/annotation/AnnotationReader.class
442 Fri Jan 29 15:35:14 PST 2016 com/sun/xml/internal/bind/v2/model/annotation/RuntimeAnnotationReader.class
9846 Fri Jan 29 15:35:22 PST 2016 com/sun/xml/internal/bind/v2/model/annotation/RuntimeInlineAnnotationReader.class
1217 Fri Jan 29 15:35:26 PST 2016 com/sun/xml/internal/ws/model/ReflectAnnotationReader$1.class
1332 Fri Jan 29 15:35:26 PST 2016 com/sun/xml/internal/ws/model/ReflectAnnotationReader$2.class
1278 Fri Jan 29 15:35:26 PST 2016 com/sun/xml/internal/ws/model/ReflectAnnotationReader$3.class
1166 Fri Jan 29 15:35:26 PST 2016 com/sun/xml/internal/ws/model/ReflectAnnotationReader$4.class
3563 Fri Jan 29 15:35:26 PST 2016 com/sun/xml/internal/ws/model/ReflectAnnotationReader.class
they are not available for your app at runtime.
However, if I run the app this way:
➜ target java -cp ./classes com.misc.common.ReadXMLFileJaxb
Book [firstName=null, lastName=Blo, age=33]
i.e. from the command line using the standard Maven structure, it does seem to run.
Your book.xml is somewhat wrong, I believe. It contains an embedded person
element whereas the Book.java does not. You may want to fix that.
Solve the error by adding these two dependencies in pom
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>