Caused by: java.lang.ClassNotFoundException: com.sun.xml.bind.v2.model.annotation.AnnotationReader

前端 未结 5 1420
暖寄归人
暖寄归人 2020-12-09 15:25

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

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-09 15:49

    As per link: Why has AnnotationReader been removed from JAXB reference implementation?, you need to simply use below maven dependencies:

    
        com.sun.xml.bind
        jaxb-impl
        2.2.11
    
    
    
        com.sun.xml.bind
        jaxb-core
        2.2.11
    
    

    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 = new ArrayList();
    
        public List getPerson() {
            return person;
        }
    
        public void setPerson(List 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
    

提交回复
热议问题