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

时间:2015-07-17 09:35:06

标签: java spring spring-mvc nosuchmethoderror

从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.xmldispatcher-servlet.xml

中是否搞砸了一些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>

我是否错过了依赖项,或者我的版本有错误?

2 个答案:

答案 0 :(得分:2)

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

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

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

尝试将此添加到pom.xml:

ServletRequestAttributes

答案 1 :(得分:-1)

我通过使用Apache httpclient的较新的4.5.9而不是4.5.6解决了此版本问题,Maven pom显示了覆盖警告。