5

After migrating from Spring 3 to Spring 4, I get the following error when I request a page:

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

I double checked and the method does not seem to exist in Spring 4. I wonder if I messed up some xsd versions in my web.xml or my dispatcher-servlet.xml

Here is my 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>

And my 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>

Am I missing a dependency or do I have some wrong versions?

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
user2035039
  • 961
  • 3
  • 16
  • 30

2 Answers2

3

This error java.lang.NoSuchMethodError: org.springframework.web.context.request.ServletRequestAttributes.<init>(Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V means that you are using the latest version of spring and you are trying to invoke older version ServletRequestAttributes from Spring 3.

Have a look in javadoc:
Spring 3 - ServletRequestAttributes
Spring 4 - ServletRequestAttributes

My guess Spring 4 uses older version of jar that contains ServletRequestAttributes. Check your libraries and see if there is conflict.

Try add this to 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>
Paweł Głowacz
  • 2,926
  • 3
  • 18
  • 25
  • All spring libraries are version 4.1.7.RELEASE and Eclipse does not show any errors – user2035039 Jul 17 '15 at 10:04
  • How do you run this application ? Maybe there is some old version libriary on web server that is messing around your House of Cards. Eclipse its one thing and runtime is other. Try to eradicate your libs from Spring 3 whatever you find them. – Paweł Głowacz Jul 17 '15 at 10:07
  • Right click on project -> Run on Server (tomcat) I noticed the following error though: One or more constraints have not been satisfied (Type: Maven Java EE Configuration Problem), but I cannot get any further information. – user2035039 Jul 17 '15 at 10:09
  • Eclipse its one thing and runtime is other. Try to eradicate your libs from Spring 3 whatever you find them. – Paweł Głowacz Jul 17 '15 at 10:11
  • Updated answer. Please add this to pom.xml. And again. – Paweł Głowacz Jul 17 '15 at 10:15
  • You can try to fix this eclipse problem with : http://stackoverflow.com/questions/23183931/maven-java-ee-configuration – Paweł Głowacz Jul 17 '15 at 10:20
  • I just did this, these problems are solved now :) Now I cannot start Tomcat however... But I'll figure it out and see if it solved my original problem. – user2035039 Jul 17 '15 at 10:26
-1

I solved this version problem by using an apache httpclient's newer 4.5.9 instead of 4.5.6, Maven pom shows an overiding warning.