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

后端 未结 5 2379
小鲜肉
小鲜肉 2020-12-10 03:44

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

相关标签:
5条回答
  • 2020-12-10 03:55

    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>
    
    0 讨论(0)
  • 2020-12-10 03:55

    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

    0 讨论(0)
  • 2020-12-10 04:06

    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.

    0 讨论(0)
  • 2020-12-10 04:09

    Below Fix Worked for me. Thanks Sven Döring

    <properties>
            <tomcat.version>8.5.37</tomcat.version>
        <properties>
    
    0 讨论(0)
  • 2020-12-10 04:16

    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'

    0 讨论(0)
提交回复
热议问题