<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # [已解決]:`javax.xml.bind.JAXBException`:`java.util.ArrayList`或其任何超類不是此上下文的已知類 > 原文: [https://howtodoinjava.com/jaxb/solved-javax-xml-bind-jaxbexception-class-java-util-arraylist-nor-any-of-its-super-class-is-known-to-this-context/](https://howtodoinjava.com/jaxb/solved-javax-xml-bind-jaxbexception-class-java-util-arraylist-nor-any-of-its-super-class-is-known-to-this-context/) 當您使用 [**JAXB**](//howtodoinjava.com/category/frameworks/jaxb/ "JAXB tutorials") 將 Java 對象(集合類型)編組為 xml 格式時,會發生此異常。 棧跟蹤如下所示: ```java Exception in thread "main" javax.xml.bind.JAXBException: class java.util.ArrayList nor any of its super class is known to this context. at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getBeanInfo(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(Unknown Source) at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source) at com.howtodoinjava.jaxb.examples.list.TestEmployeeMarshing.main(TestEmployeeMarshing.java:58) ``` ![Random exceptions](https://img.kancloud.cn/81/c9/81c97e6ebb55a2bdb88e52750e274fe3_300x350.png) 隨機異常 ## 原因 發生上述異常是因為 JAXB 總是期望實體上有一個`@XmlRootElement`注解,它會編組。 這是強制性的,不能跳過。 需要`@XmlRootElement`注解才能從 Java 對象編組的 XML 根元素中獲取元數據。 `ArrayList`類或任何 Java 集合類都沒有任何 JAXB 注解。 由于這個原因,JAXB 無法解析任何此類 java 對象,并引發此錯誤。 ## 解決方案:創建包裝器類 建議您使用此方法,因為它可以讓您靈活地在將來(例如)添加/刪除字段大小屬性。 ```java @XmlRootElement(name = "employees") @XmlAccessorType (XmlAccessType.FIELD) public class Employees { @XmlElement(name = "employee") private List<Employee> employees = null; public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } } ``` 現在,您可以按以下方式使用此類: ```java static Employees employees = new Employees(); static { employees.setEmployees(new ArrayList<Employee>()); Employee emp1 = new Employee(); emp1.setId(1); emp1.setFirstName("Lokesh"); emp1.setLastName("Gupta"); emp1.setIncome(100.0); Employee emp2 = new Employee(); emp2.setId(2); emp2.setFirstName("John"); emp2.setLastName("Mclane"); emp2.setIncome(200.0); employees.getEmployees().add(emp1); employees.getEmployees().add(emp2); } private static void marshalingExample() throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(Employees.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(employees, System.out); } Output: <employees> <employee> <id>1</id> <firstName>Lokesh</firstName> <lastName>Gupta</lastName> <income>100.0</income> </employee> <employee> <id>2</id> <firstName>John</firstName> <lastName>Mclane</lastName> <income>200.0</income> </employee> </employees> ``` 請參閱此帖子中的完整示例: [**java `ArrayList`或`Set`編組示例**](//howtodoinjava.com/jaxb/jaxb-exmaple-marshalling-and-unmarshalling-list-or-set-of-objects/ "JAXB exmaple: Marshalling and Unmarshalling list or set of objects") **祝您學習愉快!**
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看