【问题标题】:SAXParseException, the root element of the document is not <xsd:schema>SAXParseException,文档的根元素不是 <xsd:schema>
【发布时间】:2015-05-08 06:39:18
【问题描述】:

这是我的spring-security.xml 的标题:

<?xml version="1.0" encoding="UTF-8" ?>
<b:beans xmlns:b="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:sec="http://www.springframework.org/schema/security"
         xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
         xmlns:context="http://www.springframework.org/schema/context"  
         xmlns:mvc="http://www.springframework.org/schema/mvc"  

         xsi:schemaLocation="http://www.springframework.org/schema/security
          http://www.springframework.org/schema/security/spring-security-3.2.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
          http://www.springframework.org/schema/security/oauth2
          http://www.springframework.org/schema/security/spring-security-oauth2-2.0.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.2.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd  ">

启动服务器时提示这个错误:

org.xml.sax.SAXParseException: schema_reference.4: 读取失败 架构文件 'http://www.springframework.org/schema/security/spring-security-oauth2-2.0.xsd', 因为 1) 找不到文件; 2) 文件不能 读; 3) 文档的根元素不是

我对此的看法:

  1. 链接有效,文档存在

  2. 如果我将此 xsd 放在本地类路径中,我可以阅读它,甚至可以重现此错误。所以这不是网络问题。

  3. 没错,文件以&lt;xs:schema&gt; 开头,而不是&lt;xsd:schema&gt;。但来源看起来是合法的。

为什么我会收到这个错误,我该如何摆脱它?

【问题讨论】:

  • 请提供一个Minimal, Complete, and Verifiable Example (MCVE) 来说明问题。谢谢。
  • 只需像 crunchify.com/…github.com/spring-projects/spring-mvc-showcase/tree/master/src/… 这样的 spring-mvc 最小示例。然后,在 schemaLocation 中包含与版本一起列出的五个库。最后,添加 applicationContext.xml 中提供的几乎为空的 spring-security.xml。不要忘记关闭 标记。在 tomcat/jboss/jetty 服务器上启动这个应用程序。
  • 有趣的是,我在本地下载了spring-security-oauth2-2.0.xsd,将所有出现的“xs:”前缀编辑为“xsd:”并将“xsi:schemaLocation”的第6行更改为“类路径:spring-security-oauth2-2.0.xsd”,它的工作原理。但是我不再使用官方的xsd。所以这不是一个可以接受的解决方案。
  • Spring XSD 通常也在 JAR 中,并且您 schemaLocation 中 URL 的映射通过 spring-security-oauth2 进一步映射到 spring-security-oauth2-xyzjar 的 XSD 文件-xyzjar\META-INF\spring.schemas。请检查您的 JAR,可能缺少 XSD 或映射有缺陷。

标签: spring spring-mvc spring-security xsd spring-security-oauth2


【解决方案1】:

更改所有架构位置以使用无版本 URI,这些 XSD 嵌入在 Spring JAR 中。

因为您使用的是 XSD 的版本化链接,如果在类路径中找不到特定版本,您的应用程序就会中断。

See this SO answer for more details.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-29
    • 2013-10-23
    • 2018-04-19
    • 2016-04-02
    • 1970-01-01
    • 2019-06-23
    • 2015-08-18
    相关资源
    最近更新 更多