首页 > 技术文章 > NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;

fanjingfeng 2021-01-04 17:24 原文

最近把项目由原来的spring + spring mvc 升级到springboot v2.0.2上,可以启动不了,报“

NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String”

这个错。网上说是servlet-api这个jar冲突了,要使用3.1版本。所以我直接引入了:

  1.  
    <dependency>
  2.  
    <groupId>javax.servlet</groupId>
  3.  
    <artifactId>servlet-api</artifactId>
  4.  
    <version>3.1.0</version>
  5.  
    <scope>provided</scope>
  6.  
    </dependency>

再次启动发现问题如故,实际依赖的还是2.4版本。看来事情没这么简单,后来这遍博客提醒了我:http://outofmemory.cn/code-snippet/10914/maven-command-dependency。在项目根目录下执行“mvn dependency:tree”命令来查看是哪个家伙把server-api.jar引进来的:

  1.  
    [INFO] +- org.httpunit:httpunit:jar:1.7.2:compile
  2.  
    [INFO] | +- rhino:js:jar:1.6R5:compile
  3.  
    [INFO] | +- nekohtml:nekohtml:jar:0.9.5:compile
  4.  
    [INFO] | +- javax.servlet:servlet-api:jar:2.4:compile
  5.  
    [INFO] | +- net.sf.jtidy:jtidy:jar:r938:compile
  6.  
    [INFO] | +- xerces:xercesImpl:jar:2.6.1:compile
  7.  
    [INFO] | \- xerces:xmlParserAPIs:jar:2.6.1:compile

元凶是httpuint了,最后把依赖排除掉:

  1.  
    <dependency>
  2.  
    <groupId>org.httpunit</groupId>
  3.  
    <artifactId>httpunit</artifactId>
  4.  
    <version>1.7.2</version>
  5.  
    <exclusions>
  6.  
    <exclusion>
  7.  
    <groupId>javax.servlet</groupId>
  8.  
    <artifactId>servlet-api</artifactId>
  9.  
    </exclusion>
  10.  
    </exclusions>
  11.  
    </dependency>

搞定。我的问题其实不是jar冲突,准备说是ServletContext接口冲突 了。项目依赖了tomcat-embed-core-8.5.31.jar已经有ServletContext了,并且ServletContext接口确确实实定义了getVirtualServerName()方法,只不过jvm先加载了servelt-api中的ServletContext类,后来在加载tomcat-embed-core-8.5.31中的ServletContext时发现它已经存在就不再加载了。

推荐阅读