gpt4 book ai didi

java - 由 : java. lang.ClassNotFoundException : com. sun.xml.bind.v2.model.annotation.AnnotationReader 引起

转载 作者:IT老高 更新时间:2023-10-28 20:25:00 27 4
gpt4 key购买 nike

你可能认为下面的问题很简单,但我不知道我在这里做错了什么。我觉得我添加了必需的依赖项。我不是吗?

谁能告诉我这里有什么问题?

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/xml/bind/v2/model/annotation/AnnotationReader
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:242)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:441)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
at com.misc.common.ReadXMLFileJaxb.main(ReadXMLFileJaxb.java:14)
Caused by: java.lang.ClassNotFoundException: com.sun.xml.bind.v2.model.annotation.AnnotationReader
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 23 more

Book.java

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Book {
@XmlAttribute
private String firstName;
@XmlElement
private String lastName;
@XmlElement
private String age;


public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Book [firstName=" + firstName + ", lastName=" + lastName+ ", age=" + age + "]";
}
}

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.toString());
} catch (JAXBException e) {
System.out.println(e.getMessage());
}
}
}

我的书.xml

<?xml version="1.0"?>
<book>
<person>
<first>Kiran</first>
<last>Pai</last>
<age>22</age>
</person>
<person>
<first>Bill</first>
<last>Gates</last>
<age>46</age>
</person>
<person>
<first>Steve</first>
<last>Jobs</last>
<age>40</age>
</person>
</book>

enter image description here

pom.xml

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<!-- JAXB -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.12</version>
</dependency>
</dependencies>

最佳答案

根据链接:Why has AnnotationReader been removed from JAXB reference implementation? ,你需要简单地使用下面的maven依赖:

<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>

您需要稍微重构一下代码。看起来你还没有创建模型类的同名字段,它应该如下所示: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());
}
}
}

我看到以下输出:

3
------------
Kiran
Pai
22
------------
Bill
Gates
46
------------
Steve
Jobs
40

关于java - 由 : java. lang.ClassNotFoundException : com. sun.xml.bind.v2.model.annotation.AnnotationReader 引起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36557308/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com