4

当我执行代码时,我正在尝试从我的示例项目访问Opendaylight API 并获取 javax.xml.bind.UnmarshalException。

我发现,这个异常是在尝试实例化抽象类时引起的。

但是,这是来自官方来源的示例代码,我无法控制 API。我尝试用我使用 XSD 生成的类替换库中的类,结果是相同的。有人可以告诉我有什么问题或可以做些什么吗?

这是示例代码和输出。请注意,服务器配置正确,我收到了响应。

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.codec.binary.Base64;
import org.opendaylight.controller.sal.reader.FlowOnNode;
import org.opendaylight.controller.statistics.northbound.AllFlowStatistics;
import org.opendaylight.controller.statistics.northbound.FlowStatistics;

public class JAXBStatisticsClient {

    public static void main(String[] args) {

    System.out.println("Starting Statistics JAXB client.");

    String user = "admin";
    String password = "admin";
    String containerName = "default";
    String baseURL = "http://172.18.2.95:8080/controller/nb/v2/statistics";

    try {
        URL url = new java.net.URL(baseURL + "/" + containerName + "/flow");

        String authString = user + ":" + password;
        byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
        String authStringEnc = new String(authEncBytes);

        URLConnection connection = url.openConnection();
        connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
        connection.setRequestProperty("Content-Type", "application/xml");
        connection.setRequestProperty("Accept", "application/xml");
        connection.connect();

        InputStream inputStream = connection.getInputStream();
        JAXBContext context = JAXBContext.newInstance(AllFlowStatistics.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();    
        AllFlowStatistics result = (AllFlowStatistics) unmarshaller.unmarshal(inputStream);

        System.out.println("We have these statistics:");

        for (FlowStatistics statistics : result.getFlowStatistics()) {
        System.out.println("Node ID : " + statistics.getNode().getNodeIDString());
        System.out.println("Node Type : " + statistics.getNode().getType());
        if (null != statistics.getFlowStats()) {
            for (FlowOnNode flowOnNode : statistics.getFlowStats()) {
            System.out.println("\t" + flowOnNode.getByteCount());
            System.out.println("\t" + flowOnNode.getDurationNanoseconds());
            System.out.println("\t" + flowOnNode.getDurationSeconds());
            System.out.println("\t" + flowOnNode.getPacketCount());
            System.out.println("\t" + flowOnNode.getTableId());
            System.out.println("\t" + flowOnNode.getFlow());
            }
        }
        }

    } catch (Exception e) {
        System.out.println(e.getLocalizedMessage());
        e.printStackTrace();
    }
    }
}

输出

Starting Statistics JAXB client.
Unable to create an instance of org.opendaylight.controller.sal.action.Action
javax.xml.bind.UnmarshalException: Unable to create an instance of org.opendaylight.controller.sal.action.Action
 - with linked exception:
[java.lang.InstantiationException]
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StructureLoader.startElement(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
    at com.odl.JAXBStatisticsClient.main(JAXBStatisticsClient.java:42)
Caused by: java.lang.InstantiationException
    at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.sun.xml.internal.bind.v2.ClassFactory.create0(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.createInstance(Unknown Source)
    ... 21 more

XML 输出流

<?xml version="1.0" encoding="UTF-8"?>
<list>
   <flowStatistics>
      <node>
         <id>00:00:00:00:00:00:00:01</id>
         <type>OF</type>
      </node>
      <flowStatistic>
         <flow>
            <match>
               <matchField>
                  <type>DL_TYPE</type>
                  <value>2048</value>
               </matchField>
               <matchField>
                  <mask>255.255.255.255</mask>
                  <type>NW_DST</type>
                  <value>10.0.1.1</value>
               </matchField>
            </match>
            <actions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="output">
               <type>OUTPUT</type>
               <port>
                  <node>
                     <id>00:00:00:00:00:00:00:01</id>
                     <type>OF</type>
                  </node>
                  <id>3</id>
                  <type>OF</type>
               </port>
            </actions>
            <actions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="setDlDst">
               <type>SET_DL_DST</type>
               <address>000000000001</address>
            </actions>
            <priority>500</priority>
            <idleTimeout>0</idleTimeout>
            <hardTimeout>0</hardTimeout>
            <id>0</id>
         </flow>
         <tableId>0</tableId>
         <durationSeconds>8292</durationSeconds>
         <durationNanoseconds>952000000</durationNanoseconds>
         <packetCount>0</packetCount>
         <byteCount>0</byteCount>
      </flowStatistic>
      <flowStatistic>
         <flow>
            <match>
               <matchField>
                  <type>DL_TYPE</type>
                  <value>2048</value>
               </matchField>
               <matchField>
                  <type>IN_PORT</type>
                  <value>OF|3@OF|00:00:00:00:00:00:00:01</value>
               </matchField>
            </match>
            <actions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="controller">
               <type>CONTROLLER</type>
            </actions>
            <priority>500</priority>
            <idleTimeout>0</idleTimeout>
            <hardTimeout>0</hardTimeout>
            <id>0</id>
         </flow>
         <tableId>0</tableId>
         <durationSeconds>8274</durationSeconds>
         <durationNanoseconds>701000000</durationNanoseconds>
         <packetCount>0</packetCount>
         <byteCount>0</byteCount>
      </flowStatistic>
   </flowStatistics>
</list>
4

0 回答 0