異常資訊
Multiple annotations found at this line:
- schema_reference.4: Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd', because 1) could not find the
document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dubbo:reference'.
Multiple annotations found at this line:
- schema_reference.4: Failed to read schema document 'http://api.weibo.com/schema/motan.xsd', because 1) could not find the document;
2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'motan:referer'.
異常現象
在網上找了一個Spring整合Dubbo和新浪微網誌第三方登入的項目,導入工作空間兩個xml配置檔案提示上面的錯誤資訊。根據異常提示在浏覽器打開http://code.alibabatech.com/schema/dubbo/dubbo.xsd和http://api.weibo.com/schema/motan.xsd均無法打開,找到了異常原因。
解決辦法
先了解一下xml相關知識, java中的配置檔案一般配置在xml中,對于xml檔案根據不同的規範要求會對xml進行規範和檢驗,一般由dtd檔案或xsd檔案來完成。
對于dtd:
DTD(文檔類型定義)的作用是定義 XML 文檔的合法構模組化塊。
DTD 可被成行地聲明于 XML 文檔中,也可作為一個外部引用。
現在一般用xsd檔案比較多:
XML Schema 是基于 XML 的 DTD 替代者。
XML Schema 可描述 XML 文檔的結構。
XML Schema 語言也可作為 XSD(XML Schema Definition)來引用。
什麼是 XML Schema?
XML Schema 的作用是定義 XML 文檔的合法構模組化塊,類似 DTD。
XML Schema:
定義可出現在文檔中的元素
定義可出現在文檔中的屬性
定義哪個元素是子元素
定義子元素的次序
定義子元素的數目
定義元素是否為空,或者是否可包含文本
定義元素和屬性的資料類型
定義元素和屬性的預設值以及固定值
上面摘抄了網上對dtd和xsd的一些介紹,對于xml導入schema驗證這種方式隻能夠在網絡連接配接正常時才能夠使用,在網絡不穩定的情況下或Schema檔案網絡位址變化的情況,Eclipse找不到對應檔案就會提示錯誤,是以,通過設定Eclipse對Schema進行本地支援,可以解決上述問題。
下面是解決辦法:
一般公司在釋出jar包時會把需要的配置檔案以及對配置檔案校驗的檔案一塊打包到jar中,知道了這就可以到對應的jar包中找對應的xsd檔案了。
對于dubbo.xsd:
1:在本地找到對應的jar包并解壓:
2:在Eclipse中配置本地schema的支援:
依次Window--》Preferences,在左邊選擇XML--》XML Catalog--》在右邊選中User Specified Entries--》單擊Add:
确認左側選擇Catalog Entry ,在location中選擇上面解壓出來的dubbo.xsd的路徑,Key type選擇Schema location,Key中補齊Schema的名稱:
一路單擊OK,選擇項目右鍵validate,報錯消失:
對motan.xsd的報錯同了解決。
上述方法對于dtd檔案、以及代碼自動提示異常都可類似操作解決。