使用jcifs依赖时报错:The following method did not exist:javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;

使用jcifs依赖时报错:The following method did not exist:javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;

 这个错误说明你的应用程序试图调用一个不存在的方法,即 javax.servlet.ServletContext.getVirtualServerName()。

 你的环境里同时存在两个 ServletContext 类,一个在 servlet-api-2.4.jar,另一个在 tomcat-embed-core-9.0.75.jar。由于 AuthenticatorBase 是从 tomcat-embed-core-9.0.75.jar 加载的,所以它需要与之兼容的 ServletContext 版本。

 这个问题可能是由于类路径中包含的 servlet-api 版本与 tomcat-embed-core 版本不兼容所导致的。getVirtualServerName() 方法是在 Servlet 3.1 中引入的,而 servlet-api-2.4.jar 对应的 Servlet 规范版本是 2.4,这就是为什么找不到这个方法。

 要解决这个问题,你需要确保你的项目依赖关系中的 servlet-api 和 tomcat-embed-core 的版本是兼容的。通常来说,如果你在使用 Spring Boot,你不需要显式添加 servlet-api 的依赖,因为 Spring Boot 会自动管理这些依赖。如果你的项目中明确包含了 servlet-api 的依赖,试试移除它。你应该使用 Spring Boot 的 parent pom 或者使用 Spring Boot 的 starter 来管理你的依赖。

 经过问题排查,发现是该错误是下面依赖引起的

		<dependency>
            <groupId>jcifs</groupId>
            <artifactId>jcifs</artifactId>
            <version>1.3.17</version>
        </dependency>

 看起来你正在使用的 jcifs 库版本较老,它可能会间接地引入一个较老版本的 servlet-api,这可能会引发你当前遇到的问题。移除 jcifs 依赖后,问题就消失了,这进一步证实了这个猜测。

 解决这个问题的一个可能的方法是尝试更新 jcifs 到一个更新的版本,看看是否能解决这个问题。你可以尝试使用 jcifs-ng,这是一个新的库,对 jcifs 进行了现代化的更新。

 在你的 pom.xml 中,你可以把你的 jcifs 依赖替换成以下内容:

<dependency>
    <groupId>eu.agno3.jcifs</groupId>
    <artifactId>jcifs-ng</artifactId>
    <version>2.1.4</version> <!-- or latest version -->
</dependency>

 你需要替换 <version> 标签中的值为你希望使用的版本号。请查阅 jcifs-ng 的文档,以确定使用的版本是否需要其他配置。

 注意,jcifs-ngjcifs 的 API 可能有所不同,所以你可能需要对你的代码进行一些调整来适应新的库。

 如果你无法更新 jcifs 或者 jcifs-ng 不满足你的需求,另一个可能的解决方案是使用 <exclusions> 标签在 jcifs 的依赖中排除 servlet-api。例如:

<dependency>
    <groupId>jcifs</groupId>
    <artifactId>jcifs</artifactId>
    <version>1.3.17</version>
    <exclusions>
        <exclusion>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

 以上的配置将排除 jcifs 依赖中的 servlet-api。但是,请注意,如果 jcifs 确实需要 servlet-api,那么你可能需要找到一个兼容的 servlet-api 版本作为依赖添加到你的项目中。

posted @ 2023-06-25 14:21  钱有学  阅读(1034)  评论(0)    收藏  举报