Getting NoSuchMethodError:javax.servlet.ServletContext.getVirtualServerName()

前端 未结 9 1809
忘掉有多难
忘掉有多难 2020-11-28 13:03

I am facing an issue during deployment of a service in Tomcat 8. Getting following error :

Caused by: java.lang.NoSuchMethodError: javax.servlet.Ser

相关标签:
9条回答
  • 2020-11-28 13:59

    Assuming this problem appears when you ran the application in Eclipse. Use Dependency Hierarchy view to search for servlet-api in pom.xm

    0 讨论(0)
  • 2020-11-28 14:02

    The method getVirtualServerName has been added in ServletContext in Servlet 3.1. See the java doc's method getVirtualServerName.

    This problem has 3 primary causes:

    1. Your servlet version is older than 3.1.

    2. Some other jar has the servlet with a version older than 3.1.

    3. Your tomcat version is older than 8

    to solve it, you can try the below way.

    I. Check your pom.xml for the code below.

      <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>javax.servlet-api</artifactId>
           <version>3.1.0</version>
        </dependency>
    

    if your pom.xml has the above code, it would still has that problem. you can do the second way.

    II. to check your other jar has refer to the javax.servlet-api jar. for example, the org.apache.santuario has refer to the javax.servlet-api jar. the pom.xml:

    <dependency>  
        <groupId>org.apache.santuario</groupId>  
        <artifactId>xmlsec</artifactId>  
        <version>1.4.3</version>   
    </dependency> 
    

    but when you look at the maven dependencies, it refer to the javax.servlet-api jar whose version is 2.3 older than 3.1.

    so you should exclude the 2.3 version. pom.xml:

    <!-- exclude servlet-api 2.3 jar-->  
    <dependency>  
        <groupId>org.apache.santuario</groupId>  
        <artifactId>xmlsec</artifactId>  
        <version>1.4.3</version>  
        <exclusions>  
            <exclusion>  
                <groupId>javax.servlet</groupId>  
                <artifactId>servlet-api</artifactId>  
            </exclusion>  
        </exclusions>  
    </dependency>  
    
    <!-- servlet-api 3.1 version has getVirtualServerName() -->  
    <dependency>  
        <groupId>javax.servlet</groupId>  
        <artifactId>javax.servlet-api</artifactId>  
        <version>3.1.0</version>  
    </dependency> 
    

    III. spring boot run the default tomcat 7. so define your tomcat version 8 instead of tomcat 7. so add the code your pom.xml:

       <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <tomcat.version>8.5.5</tomcat.version>
        </properties>
    
    0 讨论(0)
  • 2020-11-28 14:09

    I had this error on IntelliJ with maven after updating IntelliJ.

    I could run the tests with maven but not from my IDE.

    I solved the problem by removing the ./idea and project.iml files and reloading the project.

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