java.lang.NoSuchMethodError: org.springframework.web.context.request.ServletRequestAttributes.<init>

java.lang.NoSuchMethodError: org.springframework.web.context.request.ServletRequestAttributes.<init>

从Spring 3迁移到Spring 4后,请求页面时出现以下错误:

java.lang.NoSuchMethodError: org.springframework.web.context.request.ServletRequestAttributes.<init>(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V

我仔细检查了 Spring 中似乎不存在该方法 4. 我想知道我是否搞砸了 web.xml 或 [=15= 中的某些 xsd 版本]

这是我的 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">


  <display-name>Archetype Created Web Application</display-name>

  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>
      org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
  </context-param>

  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
</web-app>

dispatcher-servlet.xml:

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

    <context:component-scan base-package="org.javacms.controllers" />

  <mvc:resources mapping="/templates/**" location="/templates/" />

  <!--  added to avoid exceptions -->
  <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
  <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

  <!-- JSTL view resolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/templates/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

还有我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>javacms</groupId>
  <artifactId>javacms</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>javacms Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <spring.version>4.1.7.RELEASE</spring.version>
    <spring-security.version>4.0.1.RELEASE</spring-security.version>
  </properties>
  <dependencies>

    <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
        </dependency>

    <!-- Spring -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${spring.version}</version>
        </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>${spring.version}</version>
        </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>


    <!-- Spring security -->
        <dependency>
          <groupId>org.springframework.security</groupId>
          <artifactId>spring-security-core</artifactId>
          <version>${spring-security.version}</version>
        </dependency>


    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-taglibs</artifactId>
      <version>${spring-security.version}</version>
    </dependency>

      <dependency>
      <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
      <version>${spring-security.version}</version>
    </dependency>


        <!-- MySQL connector --> 
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.36</version>
        </dependency>

        <!-- Hibernate --> 
        <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-core</artifactId>
          <version>4.3.10.Final</version>
        </dependency>
  </dependencies>

  <build>
    <finalName>WebApp</finalName>
  </build>
</project>

我是否遗漏了一个依赖项或者我的版本有误?

此错误 java.lang.NoSuchMethodError: org.springframework.web.context.request.ServletRequestAttributes.<init>(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V 表示您正在使用最新版本的 spring 并且您正在尝试从 Spring 调用旧版本 ServletRequestAttributes 3.

查看 javadoc:
Spring 3 - ServletRequestAttributes
Spring 4 - ServletRequestAttributes

我的猜测 Spring 4 使用包含 ServletRequestAttributes 的旧版本的 jar。检查你的库,看看是否有冲突。

尝试将此添加到 pom.xml:

  <build>
    <finalName>WebApp</finalName>
        <plugins>
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
             <source>1.7</source> 
             <target>1.7</target> 
        </configuration>
    </plugin>
    </plugins>
  </build>

我通过使用 apache httpclient 的更新版本 4.5.9 而不是 4.5.6 解决了这个版本问题,Maven pom 显示了一个覆盖警告。