【问题标题】:Spring - avoid mixing versionsSpring - 避免混合版本
【发布时间】:2019-03-19 01:38:03
【问题描述】:

在运行我的 Spring Boot 应用程序时,我收到以下错误:

尝试调用方法org.springframework.web.reactive.function.client.WebClient.builder()Lorg/springframework/web/reactive/function/client/WebClient$Builder; but it does not exist. Its class, org.springframework.web.reactive.function.client.WebClient, is available from the following locations:

jar:file:/C:/Users/Wicia/.m2/repository/org/springframework/spring-web-reactive/5.0.0.M4/spring-web-reactive-5.0.0.M4.jar!/org/springframework/web/reactive/function/client/WebClient.class

它是从以下位置加载的:

file:/C:/Users/Wicia/.m2/repository/org/springframework/spring-web-reactive/5.0.0.M4/spring-web-reactive-5.0.0.M4.jar

行动:

Correct the classpath of your application so that it contains a single, compatible version of org.springframework.web.reactive.function.client.WebClient

我发现了一条线索,我应该为所有工件指定通用的 spring 版本(而不是混合它),但是该怎么做呢?

这是我的 pom.xml:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.0.0.M4</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.0.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web-reactive</artifactId>
    <version>5.0.0.M4</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-cassandra</artifactId>
    <version>2.1.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.0.5.RELEASE</version>
</dependency>

【问题讨论】:

  • 你正在使用 Spring Boot... 然后不要乱用版本。为 spring-boot-starter-parent 使用一个版本,它应该是您的父级或 pom.xml 中 dependencyManagement 部分中的导入。所有其他版本将由 Spring Boot 管理。

标签: spring maven spring-boot spring-data


【解决方案1】:

如果依赖项由 Spring 管理,请不要在 pom.xml 中定义依赖项版本。而是使用 spring-boot-starter-parent 作为父级。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web-reactive</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-cassandra</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
<dependencies>

Spring-boot-parent 为几乎所有流行的依赖项定义了版本,因此默认情况下您可以跳过定义版本。仅当您收到 POM 错误时才定义它(意味着您添加了一个不受 Spring Boot 管理的唯一依赖项)

【讨论】:

  • 很好的解决方案,谢谢 :) 现在我添加了工件“spring-web-reactive”的版本,因为“org.springframework.web.reactive”中的一些类丢失了,但仍然会出现这个错误:/
  • 我不知道那个包是什么,但它在 mvnrepository 中的最新版本是 5.0.0.M4
  • 一般建议:不要玩版本,除非你有很好的理由并且你真的 - 真的知道你在做什么! :)
  • 你应该使用spring-webflux而不是spring-web-reactive
  • @Selindek 好的,我只是想知道这种情况 - 我想将 X 类添加到我的项目中。我在 maven 存储库中搜索它,在工件 Y 版本 Z 下找到它,现在是什么 - 如何确定我应该使用哪些版本的依赖项,哪些不应该使用? (必须有一些非常聪明的人现在该怎么做:)
猜你喜欢
  • 2017-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-27
  • 1970-01-01
  • 2014-02-21
  • 2016-08-15
  • 2017-06-06
相关资源
最近更新 更多