Spring boot: java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getHttpServletMapping()Ljavax/servlet/http/HttpServletMapping;
Asked Answered
R

8

28

When I run this application on built-in server or other tomcat server it gives me following error message. I am using jdk8, STS V-3.9.7. Can someone help me resolve it

Simple Springboot application with following content:

application.properties file for view mapping

    spring.mvc.view.prefix=/WEB-INF/jspViews/
    spring.mvc.view.suffix=.jsp

index.jsp

    <body>
       <h3>Application is working</h3>
     </body>

Controller class

    @Controller
    public class AppController {    
       @RequestMapping("/")
       public String indexPage() {
           return "index";
       }
    }

pom.xml file

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>  
    <groupId>com.demo</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>ehospitalsso</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

web.xml file

 <?xml version="1.0" encoding="UTF-8"?>
    <web-app 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">

      <display-name>demo</display-name>
      <welcome-file-list>   
        <welcome-file>index.jsp</welcome-file>    
      </welcome-file-list>
    </web-app>

Error StackTrace ::

java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getHttpServletMapping()Ljavax/servlet/http/HttpServletMapping;
    at org.apache.catalina.core.ApplicationHttpRequest.setRequest(ApplicationHttpRequest.java:690) ~[catalina.jar:na]
    at org.apache.catalina.core.ApplicationHttpRequest.<init>(ApplicationHttpRequest.java:114) ~[catalina.jar:na]
    at org.apache.catalina.core.ApplicationDispatcher.wrapRequest(ApplicationDispatcher.java:917) ~[catalina.jar:na]
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:358) ~[catalina.jar:na]
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:312) ~[catalina.jar:na]
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:170) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:316) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1370) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1116) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1055) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[servlet-api.jar:na]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[servlet-api.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [catalina.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:na]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-websocket.jar:9.0.13-dev]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:na]
    at org.springframework.boot.web.servlet.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:130) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
    at org.springframework.boot.web.servlet.support.ErrorPageFilter.access$000(ErrorPageFilter.java:66) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
    at org.springframework.boot.web.servlet.support.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:105) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.boot.web.servlet.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:123) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:na]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) [spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:na]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) [catalina.jar:na]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [catalina.jar:na]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) [catalina.jar:na]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [catalina.jar:na]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [catalina.jar:na]
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668) [catalina.jar:na]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [catalina.jar:na]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [catalina.jar:na]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-coyote.jar:9.0.13-dev]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-coyote.jar:9.0.13-dev]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791) [tomcat-coyote.jar:9.0.13-dev]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417) [tomcat-coyote.jar:9.0.13-dev]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-coyote.jar:9.0.13-dev]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_161]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_161]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:9.0.13-dev]
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_161]

Screen Sort:

enter image description here

Kind attend Mr.Avijit for update pox.xml file

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>        
    </parent>
    <groupId>com.demo</groupId>
    <artifactId>Demo_App</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>Demo_App</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>      

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

ServletInitializer Class

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(DemoAppApplication.class);
    }

}

DemoAppApplication class

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoAppApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoAppApplication.class, args);
    }

}
Rappel answered 10/1, 2019 at 6:0 Comment(13)
Can you give a screenshot of your project structure ?Electronegative
Hi Avijit, I have update it with screenshot. Please help?Rappel
Can you please check my answer ?Electronegative
Ramesh remove <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> this dependency and add all dependency in my answerElectronegative
Try deleting the line <relativePath/> <!-- lookup parent from repository --> in your pom.xml.Chui
Dear Avijit, It is giving same error.Rappel
@ Avijit Barua: Dear sir, post updated with updated pom.xmlRappel
Please post your main class and servlet initializerElectronegative
Could you please now...Rappel
You haven't posted your DemoAppApplication.class and servlet initializer classElectronegative
DemoAppApplication.class and servlet initializer class postedRappel
@RameshKumar Check my answer step by step. And let us knowElectronegative
@RameshKumar Have you checked ?Electronegative
Q
22

I think there is a version mismatch (possibly a Bug?) in Spring Boot.

Spring Boot 2.1.X uses Tomcat 9 which has the Servlet API v4.
But Spring Boot Web 2.1.X still incorporates Servlet API v3.1.

You should solve the problem by downgrading the spring-boot-starter-tomcat to version 2.0.X.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <version>2.0.7.RELEASE</version>
        <scope>test</scope>
    </dependency>

Edit:
It might as well work just to change the Tomcat version property.

    <properties>
        <tomcat.version>8.5.37</tomcat.version>
    <properties>
Quirita answered 22/1, 2019 at 22:55 Comment(4)
Your first suggestion worked for me (downgraded from Spring Boot 2.1.3.RELEASE). Thanks! .. For the second option, where would those lines go?Cribb
The tomcat.version property is a normal Maven property in your pom.xml. Just add the tomcat.version to your existing Maven properties.Deva
Thanks! This resolved my problem. I think the property setting is a nicer way of mending.Tuckie
Please remember that second solution (although IMHO is more elegant) will only work if your parent pom is spring-boot-starter-parent. Use the first solution if you are using different parent pom and Spring Boot is added manually to your dependencies.Octodecimo
B
5

I had a similar problem and the cause was the jsp-api didn't fit to the corresponding servlet-api version. As Sven Döring already mentioned Spring Boot 2.1.x uses Tomcat 9 which uses/provides Servlet API v4. For this you need jsp-api 2.3.3 otherwise you can get the NoSuchMethodError.

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.3</version>
        <scope>provided</scope>
    </dependency>

More info about the version matching can be found here: The ultimate Java version table (J2EE, Java EE, Servlet, JSP, JSTL) Unfortunately Tomcat 9 and Servlet-API 4 is missing in this list.

Bondmaid answered 7/2, 2019 at 10:57 Comment(0)
A
5

I'd like to add to answer of Sven Döring solution for gradle:

compile(group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springVer) {
    exclude module: "spring-boot-starter-tomcat"
}
compile(group: 'org.springframework.boot', name: 'spring-boot-starter-tomcat', version: springTomcatVer)

At this point, I set the params:

springVer='2.1.8.RELEASE'
springTomcatVer='2.0.9.RELEASE'

Armoured answered 20/9, 2019 at 6:1 Comment(0)
S
1

merge your DemoAppApplication with servlet initializer as follow:

@SpringBootApplication
public class DemoAppApplication extends SpringBootServletInitializer{

public static void main(String[] args) {
    SpringApplication.run(DemoAppApplication.class, args);
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(DemoAppApplication.class);
}

}

or add @Configuration annotation to class that extends SpringBootServletInitializer

Scrotum answered 23/1, 2019 at 7:8 Comment(0)
S
1

Below Fix Worked for me. Thanks Sven Döring

<properties>
        <tomcat.version>8.5.37</tomcat.version>
    <properties>
Subsume answered 17/2, 2020 at 10:23 Comment(1)
Please consider deleting your answer and then upvoting the answer your referenced: meta.stackexchange.com/questions/126180/…Swaraj
N
0

because servlet-api version not match tomcat version. if you use tomcat 9.x should check your servlet-api version over the v4 also when you use tomcat 8.x should use servlet-api v3

Neurosurgery answered 15/12, 2021 at 3:12 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Infusive
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewDisclimax
L
0

For anyone still stuck after the above suggestions:

My issue was due to including javax.servlet:javax.servlet-api:3.1.0 in my build.gradle file dependencies (although the same issue could occur with maven).

If you're using embedded Tomcat, the javax library is included in the tomcat-embed-core jar which is found in the maven library spring-boot-starter-tomcat. Remove the reference to javax.servlet:javax.servlet-api:3.1.0.

Levorotation answered 10/2, 2022 at 21:1 Comment(0)
A
-1

I fixed the issue by adding the servlet_api.jar to project lib path from apache-tomcat-9.0.27\lib\servlet_api.jar.

Ankylostomiasis answered 19/2, 2021 at 15:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.