spring-boot-dependencies 与spring-boot-starter-web冲突?

我的项目root目录下pom文件添加了springboot的依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.6.3</version>
            <type>pom</type>
            <scope>import</scope>
</dependency>

前端的目录下pom中添加了,要用到@restcontroller

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.6.3</version>
        </dependency>

然后一堆包冲突,是不是我的做法有错误,还是版本问题?

阅读 2.3k
1 个回答

spring-boot-dependencies 和 spring-boot-starter-web 并不会发生冲突,它们分别是 Spring Boot 提供的依赖管理和 Web Starter,可以在一个项目中同时使用。

spring-boot-dependencies 是一个 Maven BOM(Bill of Materials),包含了所有 Spring Boot 项目中使用的依赖的版本信息,可以通过继承该 BOM 来轻松管理版本。它是一个空的 Maven 项目,只包含一个 pom.xml 文件,用于统一管理 Spring Boot 的依赖版本号。您可以在自己的项目中添加 BOM 依赖,例如:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-dependencies</artifactId>
      <version>2.6.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

spring-boot-starter-web 则是 Spring Boot Web Starter,包含了一些常用的 Web 组件,比如 Spring MVC、Tomcat、Jackson 等。通过添加该依赖,可以快速构建基于 Spring MVC 的 Web 项目。您可以在 Maven 项目中添加如下依赖:

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
</dependencies>

如果您在添加 spring-boot-starter-web 依赖时发生冲突,可能是由于您在项目中同时添加了其他的 Web 框架、Servlet 容器或者 Spring Boot 的其他 Starter,这些依赖会导致版本冲突。您可以使用 mvn dependency:tree 命令查看项目依赖树,找到冲突的依赖,并将其排除或者升级版本。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题