原文: https://howtodoinjava.com/jaxb/solved-exception-in-thread-main-com-sun-xml-internal-bind-v2-runtime-illegalannotationsexception-3-counts-of-illegalannotationexceptions/

当您使用 JAXB 将 Java 对象(集合类型)编组为 xml 格式时,会发生此异常。 栈跟踪如下所示:

  1. Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
  2. Class has two properties of the same name "employees"
  3. this problem is related to the following location:
  4. at public java.util.List com.howtodoinjava.jaxb.examples.list.Employees.getEmployees()
  5. at com.howtodoinjava.jaxb.examples.list.Employees
  6. this problem is related to the following location:
  7. at private java.util.List com.howtodoinjava.jaxb.examples.list.Employees.employees
  8. at com.howtodoinjava.jaxb.examples.list.Employees
  9. at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(Unknown Source)
  10. at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(Unknown Source)
  11. at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source)
  12. at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source)
  13. at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(Unknown Source)
  14. ..... more

或者

  1. Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 3 counts of IllegalAnnotationExceptions
  2. java.util.Map is an interface, and JAXB can't handle interfaces.
  3. this problem is related to the following location:
  4. at java.util.Map
  5. at private java.util.Map com.howtodoinjava.jaxb.examples.map.EmployeeMap.employeeMap
  6. at com.howtodoinjava.jaxb.examples.map.EmployeeMap
  7. java.util.Map does not have a no-arg default constructor.
  8. this problem is related to the following location:
  9. at java.util.Map
  10. at private java.util.Map com.howtodoinjava.jaxb.examples.map.EmployeeMap.employeeMap
  11. at com.howtodoinjava.jaxb.examples.map.EmployeeMap
  12. Class has two properties of the same name "employeeMap"
  13. this problem is related to the following location:
  14. at public java.util.Map com.howtodoinjava.jaxb.examples.map.EmployeeMap.getEmployeeMap()
  15. at com.howtodoinjava.jaxb.examples.map.EmployeeMap
  16. this problem is related to the following location:
  17. at private java.util.Map com.howtodoinjava.jaxb.examples.map.EmployeeMap.employeeMap
  18. at com.howtodoinjava.jaxb.examples.map.EmployeeMap
  19. ......more

[已解决]:线程“`main`”`com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException`中的异常:3 个`IllegalAnnotationExceptions`计数 - 图1

随机异常

原因

发生上述异常的主要原因是缺少@XmlAccessType注解或对@XmlAccessType@XxmlElement注解的无效使用。 正确的用法是,一个 Java 字段应只包含一个表示其元数据的有效 JAXB 注解。

默认情况下,JAXB 包含所有公开字段和用于整理的获取器。 因此,如果您有一个字段及其获取器,那么它将包含两次。 这是错误,需要通过正确使用注解来解决。

解决方案:使用@XmlAccessType注解

1)@XmlAccessorType(XmlAccessType.FIELD)

如果您使用的是XmlAccessType.FIELD;那么只有所有公开字段(非静态)将被自动包括在内以进行编组。 不会考虑吸气剂。 因此,如果将消除重复的问题。 例如,在下面的代码中,将同时包含employeesize字段。

请注意,“@XmlElement(name="employee")”是可选的; 我用它来重命名输出 xml 中的 xml 节点。 如果删除它; 不会出现任何编组错误或异常。

  1. @XmlRootElement(name = "employees")
  2. @XmlAccessorType (XmlAccessType.FIELD)
  3. public class Employees
  4. {
  5. @XmlElement(name="employee")
  6. private List<Employee> employees = null;
  7. private Integer size;
  8. public List<Employee> getEmployees() {
  9. return employees;
  10. }
  11. public void setEmployees(List<Employee> employees) {
  12. this.employees = employees;
  13. }
  14. public Integer getSize() {
  15. return size;
  16. }
  17. public void setSize(Integer size) {
  18. this.size = size;
  19. }
  20. }

2)@XmlAccessorType(XmlAccessType.NONE)

如果使用“XmlAccessType.NONE”,则意味着必须在输出 XML 中注解所有要编组的字段。 剩下的任何字段都不会包含在 JAXB 上下文中。 因此,本质上,employeesize字段都需要@XmlElement注解。 如果这两个字段中的任何一个都未使用@XmlElement注解,则不会将其编组。

  1. @XmlRootElement(name = "employees")
  2. @XmlAccessorType (XmlAccessType.NONE)
  3. public class Employees
  4. {
  5. @XmlElement(name="employee")
  6. private List<Employee> employees = null;
  7. @XmlElement(name="size")
  8. private Integer size;
  9. public List<Employee> getEmployees() {
  10. return employees;
  11. }
  12. public void setEmployees(List<Employee> employees) {
  13. this.employees = employees;
  14. }
  15. public Integer getSize() {
  16. return size;
  17. }
  18. public void setSize(Integer size) {
  19. this.size = size;
  20. }
  21. }

祝您学习愉快!