You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用含Sort.by的findAll方法时遭遇java.lang.NoSuchMethodError问题求助

问题分析与解决

错误原因

  • 项目中同时引入了两个冲突的Spring Data Commons相关依赖:spring-data-commons:2.1.3.RELEASE 和 spring-data-commons-core:1.4.1.RELEASE。
  • spring-data-commons-core 是Spring Data早期版本的核心包,在Spring Data 1.x之后就被spring-data-commons取代。旧版本的Sort类中不存在Sort.by(Direction, String...)这个静态方法,导致运行时因找不到对应方法抛出NoSuchMethodError。

解决方案

直接移除spring-data-commons-core依赖,只保留spring-data-commons:2.1.3.RELEASE即可。修改后的pom依赖片段如下:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-commons</artifactId>
    <version>2.1.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
</dependency>

额外提示

  • Sort.by(Direction, String...)方法是Spring Data Commons 2.x版本新增的,必须保证项目中只有新版本的spring-data-commons包被加载,避免类加载时优先使用旧版本的Sort类。
  • 如果移除旧依赖后问题仍存在,可执行mvn dependency:tree命令排查是否有其他依赖间接引入了旧版本的spring-data-commons-core,若有则通过<exclusions>标签排除该传递依赖。

内容的提问来源于stack exchange,提问作者Sakthi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 06:05:38