异常信息
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文件、以及代码自动提示异常都可类似操作解决。