Spring Boot(Maven)中的不兼容依赖项

Posted

技术标签:

【中文标题】Spring Boot(Maven)中的不兼容依赖项【英文标题】:Incompatible dependencies in Spring Boot (Maven) 【发布时间】:2021-05-15 17:06:30 【问题描述】:

我已经开始使用Spring Boot 创建一个 RESTful API。我已经有 2 年没碰过Spring 了。我正在使用Maven。我有主课:

package com.tsakirogf.smartapi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SmartapiApplication


    public static void main(String[] args)
    
        SpringApplication.run(PeopleapiApplication.class, args);
    


我有一个ControllerModelService。这是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.tsakirogf</groupId>
    <artifactId>peopleapi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>smartapi</name>
    <description>Technical Test</description>
    <properties>
        <java.version>11</java.version>
<!--        <tomcat.version>8.5.63</tomcat.version>-->
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.200</version>
<!--            <scope>test</scope>-->
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.4.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
            <version>2.3.6.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

项目构建成功,mvn cleanmvn install 命令也成功运行。 问题是当我尝试在IntelliJ 中运行它时,我收到了这个错误:

2021-02-11 18:19:42.692 ERROR 14584 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    org.apache.catalina.authenticator.AuthenticatorBase.startInternal(AuthenticatorBase.java:1355)

The following method did not exist:

    'java.lang.String javax.servlet.ServletContext.getVirtualServerName()'

The method's class, javax.servlet.ServletContext, is available from the following locations:

    jar:file:/F:/Workspace/Uni/SoftwareEngineer/peopleapi/lib/javax.servlet.jar!/javax/servlet/ServletContext.class
    jar:file:/C:/Users/Fotis/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.41/tomcat-embed-core-9.0.41.jar!/javax/servlet/ServletContext.class

The class hierarchy was loaded from the following locations:

    javax.servlet.ServletContext: file:/F:/Workspace/Uni/SoftwareEngineer/peopleapi/lib/javax.servlet.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of javax.servlet.ServletContext


Process finished with exit code 1

我有什么想法或方向吗?我尝试过同步依赖并尝试了pom 中的最新和RELEASE 包。我还删除了整个m2 repo(两次)。

【问题讨论】:

你的问题是F:/Workspace/Uni/SoftwareEngineer/peopleapi/lib/javax.servlet.jar。检查为什么你的类路径中有那个 JAR(很可能它是通过一些脚本来的)。这似乎比您在 pom.xml 中引用的版本旧 @dunni 修复它,dunni。你能把它作为答案发布吗?你应该得到这些点。非常感谢,我真的很感激。我想知道为什么 mvn clean 没有清除那个 lib 文件夹。这是我的假设。 【参考方案1】:

您的问题是 F:/Workspace/Uni/SoftwareEngineer/peopleapi/lib/javax.servlet.jar。检查为什么你的类路径中有那个 JAR(很可能它是通过一些脚本来的)。这似乎比您在 pom.xml 中引用的版本旧

【讨论】:

以上是关于Spring Boot(Maven)中的不兼容依赖项的主要内容,如果未能解决你的问题,请参考以下文章

spring boot应用程序作为maven依赖项[重复]

IntelliJ IDEA打开Maven项目,Spring boot所有依赖红名,不可用

错误运行Spring Boot(通过字段表示的不满足的依赖关系)[重复]

Spring Boot 1.4:Spring Data Cassandra 1.4.2 与 Cassandra 3.0 不兼容?

sprint boot 自动创建web应用

Spring BootSpring Boot之两种引入spring boot maven依赖的方式