获取 NoSuchMethodError:javax.servlet.ServletContext.getVirtualServerName()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34950164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:10:17  来源:igfitidea点击:

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

javaservletstomcat8

提问by Rehman

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

我在 Tomcat 8 中部署服务期间遇到问题。出现以下错误:

Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String; at org.apache.tomcat.websocket.server.WsServerContainer.(WsServerContainer.java:149) at org.apache.tomcat.websocket.server.WsSci.init(WsSci.java:131) at org.apache.tomcat.websocket.server.WsSci.onStartup(WsSci.java:47) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5244) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ... 10 more

引起:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String; 在 org.apache.tomcat.websocket.server.WsServerContainer.(WsServerContainer.java:149) 在 org.apache.tomcat.websocket.server.WsSci.init(WsSci.java:131) 在 org.apache.tomcat.websocket。 server.WsSci.onStartup(WsSci.java:47) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5244) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ……还有 10 个

Method getVirtualServerNamewas introduced in Servlet 3.1 and after extracting MANIFEST.MFfrom my servlet-apijar I got following details :

方法getVirtualServerName是在 Servlet 3.1 中引入的,MANIFEST.MF从我的servlet-apijar 中提取后,我得到了以下详细信息:

Specification-Title: Java API for Servlets 
Specification-Version: 3.1 
Specification-Vendor: Sun Microsystems, Inc. 
Implementation-Title: javax.servlet 

Which says that its having 3.1. So is there any other reason for this error? Please help

其中说它有 3.1。那么这个错误还有其他原因吗?请帮忙

回答by Andrew Regan

Check all your Maven (or equivalent) dependencies and make sure that you - or most likely another dependency - are not pulling in a pre-3.1 version of the javax.servlet / servlet-apithat may be taking precedence over what's in your Tomcat 8. If you've manually deployed, make sure you haven't manually copied any servlet-api JARs into Tomcat itself.

检查您的所有 Maven(或等效的)依赖项,并确保您(或很可能是另一个依赖项)没有引入 3.1 之前的版本,该版本javax.servlet / servlet-api可能优先于 Tomcat 8 中的内容。如果您已手动部署,请确保您没有将任何 servlet-api JAR 手动复制到 Tomcat 本身中。

See: https://stackoverflow.com/a/26232535/954442

参见:https: //stackoverflow.com/a/26232535/954442

回答by MrSimpleMind

Spring boot will run tomcat 7per default, you have to override maven build tomcat.version in your pom.xml. See below to run tomcat 8.0.30

Spring boot 将默认运行tomcat 7,您必须在 pom.xml 中覆盖 maven build tomcat.version。见下文运行tomcat 8.0.30

<properties>
  <tomcat.version>8.0.30</tomcat.version>
</properties>

Should fix your problem.

应该解决你的问题。

回答by Ankur

SolvedOn my mac with java 8 was facing issue with downloaded tomcat from site and unzip.

解决了在我的带有 java 8 的 mac 上遇到了从站点下载的 tomcat 并解压缩的问题。

My issue got solved because there was a extra servlet-api.jar file which was getting picked up. It was coming from /Library/Java/Extensions/servlet-api.jar

我的问题得到了解决,因为有一个额外的 servlet-api.jar 文件正在被提取。它来自 /Library/Java/Extensions/servlet-api.jar

For finding it in your system you can use sudo find / -name servlet-api.jar

要在您的系统中找到它,您可以使用 sudo find / -name servlet-api.jar

Removed it by backing it up somewhere else.

通过将其备份到其他地方来删除它。

I was following this for intallation https://gist.github.com/ddanailov-nmdp/c97aba2ca926b9627f6b4f7174083a32

我正在关注这个安装 https://gist.github.com/ddanailov-nmdp/c97aba2ca926b9627f6b4f7174083a32

回答by zlandorf

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

更新 IntelliJ 后,我在使用 Maven 的 IntelliJ 上出现此错误。

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

我可以使用 maven 运行测试,但不能从我的 IDE 中运行。

I solved the problem by removing the ./ideaand project.imlfiles and reloading the project.

我通过删除./ideaproject.iml文件并重新加载项目解决了这个问题。

回答by Do Will

This surely has something to do with the version of javax.servlet and version of Tomcat.

这肯定与javax.servlet的版本和Tomcat的版本有关。

In my case, it went away when I declared javax.servlet dependency in gradle with no version. Like this -

就我而言,当我在没有版本的 gradle 中声明 javax.servlet 依赖项时,它就消失了。像这样 -

compile('javax.servlet:servlet-api')

回答by Java Basketball

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

该方法getVirtualServerName已在 Servlet 3.1 中的 ServletContext 中添加。请参阅 java 文档的方法getVirtualServerName

This problem has 3 primary causes:

此问题有 3 个主要原因:

  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

  1. 您的 servlet 版本早于 3.1。

  2. 其他一些 jar 的 servlet 版本早于 3.1。

  3. 您的 tomcat 版本早于 8

to solve it, you can try the below way.

要解决它,您可以尝试以下方法。

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

I. 检查您的 pom.xml 以获取以下代码。

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

如果你的 pom.xml 有上面的代码,它仍然会有这个问题。你可以用第二种方法。

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

二、检查您的其他 jar 是否引用了该javax.servlet-apijar。例如,the org.apache.santuariohas 指的是javax.servlet-apijar。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-apijar whose version is 2.3 older than 3.1.

但是当您查看 maven 依赖项时,它指的javax.servlet-api是版本为 2.3 早于 3.1的jar。

enter image description here

在此处输入图片说明

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

所以你应该排除 2.3 版本。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:

三、spring boot 运行默认的 tomcat 7. 所以定义你的 tomcat version 8 而不是 tomcat 7. 所以添加你的 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>

回答by user2008914

After a huge pain & sifting through all these stackoverflow answers the only thing that ended up working for me was downgrading from tomcat8 to tomcat7. I know this isn't an ideal solution, and perhaps it was just a fresh install of tomcat that solved my problem. If all else fails give that a shot.

在经历了巨大的痛苦并筛选了所有这些 stackoverflow 答案之后,最终对我有用的唯一一件事就是从 tomcat8 降级到 tomcat7。我知道这不是一个理想的解决方案,也许只是全新安装的 tomcat 解决了我的问题。如果所有其他方法都失败了,请试一试。

回答by yuen26

If you have used this dependency:

如果您使用了此依赖项:

<dependency>
    <groupId>com.google.oauth-client</groupId>
    <artifactId>google-oauth-client-jetty</artifactId>
    <version>1.23.0</version>
</dependency>

Then please exclude as below:

那么请排除如下:

<dependency>
    <groupId>com.google.oauth-client</groupId>
    <artifactId>google-oauth-client-jetty</artifactId>
    <version>1.23.0</version>
    <exclusions>
        <exclusion>
            <artifactId>servlet-api</artifactId>
            <groupId>org.mortbay.jetty</groupId>
        </exclusion>
    </exclusions>
</dependency>