Spring Cloud (4) | NoSuchMethodError:javax.servlet.ServletContext.getVirtualServerName()

来源:互联网 发布:舞蹈mmd软件 编辑:程序博客网 时间:2024/06/08 20:10

the method getVirtualServerName has been added in ServletContext in Servlet 3.1. Find the java doc’s method getVirtualServerName

this problem can have at least 3 causes:

  1. your servlet version is older that 3.1.

  2. other jar has the servlet older version than 3.1.

  3. your tomcat version is older than 8

to solve it, you can try the below way.

I. to check your pom.xml whether there are the below code.

  <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.

enter image description here

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>

原文链接:https://stackoverflow.com/questions/34950164/getting-nosuchmethoderrorjavax-servlet-servletcontext-getvirtualservername/47503564#47503564

阅读全文
0 0
原创粉丝点击