ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# [已解决]:线程“`main`”`com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException`中的异常:3 个`IllegalAnnotationExceptions`计数 > 原文: [https://howtodoinjava.com/jaxb/solved-exception-in-thread-main-com-sun-xml-internal-bind-v2-runtime-illegalannotationsexception-3-counts-of-illegalannotationexceptions/](https://howtodoinjava.com/jaxb/solved-exception-in-thread-main-com-sun-xml-internal-bind-v2-runtime-illegalannotationsexception-3-counts-of-illegalannotationexceptions/) 当您使用 [**JAXB**](//howtodoinjava.com/category/frameworks/jaxb/ "JAXB tutorials") 将 Java 对象(集合类型)编组为 xml 格式时,会发生此异常。 栈跟踪如下所示: ```java Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions Class has two properties of the same name "employees" this problem is related to the following location: at public java.util.List com.howtodoinjava.jaxb.examples.list.Employees.getEmployees() at com.howtodoinjava.jaxb.examples.list.Employees this problem is related to the following location: at private java.util.List com.howtodoinjava.jaxb.examples.list.Employees.employees at com.howtodoinjava.jaxb.examples.list.Employees at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(Unknown Source) ..... more ``` 或者 ```java Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 3 counts of IllegalAnnotationExceptions java.util.Map is an interface, and JAXB can't handle interfaces. this problem is related to the following location: at java.util.Map at private java.util.Map com.howtodoinjava.jaxb.examples.map.EmployeeMap.employeeMap at com.howtodoinjava.jaxb.examples.map.EmployeeMap java.util.Map does not have a no-arg default constructor. this problem is related to the following location: at java.util.Map at private java.util.Map com.howtodoinjava.jaxb.examples.map.EmployeeMap.employeeMap at com.howtodoinjava.jaxb.examples.map.EmployeeMap Class has two properties of the same name "employeeMap" this problem is related to the following location: at public java.util.Map com.howtodoinjava.jaxb.examples.map.EmployeeMap.getEmployeeMap() at com.howtodoinjava.jaxb.examples.map.EmployeeMap this problem is related to the following location: at private java.util.Map com.howtodoinjava.jaxb.examples.map.EmployeeMap.employeeMap at com.howtodoinjava.jaxb.examples.map.EmployeeMap ......more ``` ![Random exceptions](https://img.kancloud.cn/81/c9/81c97e6ebb55a2bdb88e52750e274fe3_300x350.png) 随机异常 ## 原因 发生上述异常的主要原因是缺少`@XmlAccessType`注解或对`@XmlAccessType`和`@XxmlElement`注解的无效使用。 正确的用法是,一个 Java 字段应只包含一个表示其元数据的有效 JAXB 注解。 默认情况下,JAXB 包含所有公开字段和用于整理的获取器。 因此,如果您有一个字段及其获取器,那么它将包含两次。 这是错误,需要通过正确使用注解来解决。 ## 解决方案:使用`@XmlAccessType`注解 **1)`@XmlAccessorType(XmlAccessType.FIELD)`** 如果您使用的是`XmlAccessType.FIELD;`那么只有所有公开字段(非静态)将被自动包括在内以进行编组。 不会考虑吸气剂。 因此,如果将消除重复的问题。 例如,在下面的代码中,将同时包含`employee`和`size`字段。 请注意,“`@XmlElement(name="employee")`”是可选的; 我用它来重命名输出 xml 中的 xml 节点。 如果删除它; 不会出现任何编组错误或异常。 ```java @XmlRootElement(name = "employees") @XmlAccessorType (XmlAccessType.FIELD) public class Employees { @XmlElement(name="employee") private List<Employee> employees = null; private Integer size; public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } public Integer getSize() { return size; } public void setSize(Integer size) { this.size = size; } } ``` **2)`@XmlAccessorType(XmlAccessType.NONE)`** 如果使用“`XmlAccessType.NONE`”,则意味着必须在输出 XML 中注解所有要编组的字段。 剩下的任何字段都不会包含在 JAXB 上下文中。 因此,本质上,`employee`和`size`字段都需要`@XmlElement`注解。 如果这两个字段中的任何一个都未使用`@XmlElement`注解,则不会将其编组。 ```java @XmlRootElement(name = "employees") @XmlAccessorType (XmlAccessType.NONE) public class Employees { @XmlElement(name="employee") private List<Employee> employees = null; @XmlElement(name="size") private Integer size; public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } public Integer getSize() { return size; } public void setSize(Integer size) { this.size = size; } } ``` **祝您学习愉快!**