Spring 4.2 版本支持 CORS ,配置一直报“Cannot locate … element [cors]”???

头部声明:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

配置:

<mvc:cors> <mvc:mapping path="/api/v1/**" allowed-origins="*" allowed-methods="POST, GET, OPTIONS, DELETE, PUT" allowed-headers="Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With" allow-credentials="true" />
</mvc:cors>

报错:

严重: StandardWrapper.Throwable
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Cannot locate BeanDefinitionParser for element [cors]
Offending resource: ServletContext resource [/WEB-INF/springMVC-servlet.xml] at org.springframework.beans.factory.parsing.FailFastProblemReporter.fatalFailFastProblemReporter.java:60 at org.springframework.beans.factory.parsing.ReaderContext.fatalReaderContext.java:68 at org.springframework.beans.factory.parsing.ReaderContext.fatalReaderContext.java:55 at org.springframework.beans.factory.xml.NamespaceHandlerSupport.findParserForElementNamespaceHandlerSupport.java:85 at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parseNamespaceHandlerSupport.java:74 at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElementBeanDefinitionParserDelegate.java:1424 at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElementBeanDefinitionParserDelegate.java:1414 at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitionsDefaultBeanDefinitionDocumentReader.java:189 at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitionsDefaultBeanDefinitionDocumentReader.java:143 at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitionsDefaultBeanDefinitionDocumentReader.java:110 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitionsXmlBeanDefinitionReader.java:508 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitionsXmlBeanDefinitionReader.java:391 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitionsXmlBeanDefinitionReader.java:335 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitionsXmlBeanDefinitionReader.java:303 at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitionsAbstractBeanDefinitionReader.java:180 at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitionsAbstractBeanDefinitionReader.java:216 at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitionsAbstractBeanDefinitionReader.java:187 at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitionsXmlWebApplicationContext.java:125 at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitionsXmlWebApplicationContext.java:94 at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactoryAbstractRefreshableApplicationContext.java:129 at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactoryAbstractApplicationContext.java:537 at org.springframework.context.support.AbstractApplicationContext.refreshAbstractApplicationContext.java:452 at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContextFrameworkServlet.java:658 at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContextFrameworkServlet.java:624 at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContextFrameworkServlet.java:672 at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContextFrameworkServlet.java:543 at org.springframework.web.servlet.FrameworkServlet.initServletBeanFrameworkServlet.java:484 at org.springframework.web.servlet.HttpServletBean.initHttpServletBean.java:136 at javax.servlet.GenericServlet.initGenericServlet.java:158 at org.apache.catalina.core.StandardWrapper.initServletStandardWrapper.java:1282 at org.apache.catalina.core.StandardWrapper.loadServletStandardWrapper.java:1195 at org.apache.catalina.core.StandardWrapper.loadStandardWrapper.java:1085 at org.apache.catalina.core.StandardContext.loadOnStartupStandardContext.java:5318 at org.apache.catalina.core.StandardContext.startInternalStandardContext.java:5610 at org.apache.catalina.util.LifecycleBase.startLifecycleBase.java:147 at org.apache.catalina.core.ContainerBase$StartChild.callContainerBase.java:1572 at org.apache.catalina.core.ContainerBase$StartChild.callContainerBase.java:1562 at java.util.concurrent.FutureTask.runUnknown Source at java.util.concurrent.ThreadPoolExecutor.runWorkerUnknown Source at java.util.concurrent.ThreadPoolExecutor$Worker.runUnknown Source at java.lang.Thread.runUnknown Source

求大神解决??

这个困扰了我一整天的问题,我今天上午解决了。
原因:

我在升级4.2版本的时候,并没有直接把`WebContent/WEB-INF/lib`下的 Spring 4.1 版本删除掉。导致了jar包冲突。

疑问:

其实我在项目中并没有导4.1版本的jar包,为什么会冲突?Tomcat是默认加载的lib下面所有的包?

我猜会不会是xml头部的schema声明不对啊?你检查下

*xml头部的schema声明不对*

发表评论

电子邮件地址不会被公开。 必填项已用*标注