添加Spring HATEOAS到POM后创建slicedResourcesAssembler Bean失败
问题解决办法
核心原因
Spring Boot 3.1.0对应的Spring HATEOAS版本是2.x,org.springframework.hateoas.SlicedModel在2.x版本中已被移除,替代类为org.springframework.hateoas.SliceModel。直接引入spring-hateoas依赖容易导致版本不匹配,引发类找不到的异常。
具体修复步骤
替换为Spring Boot HATEOAS Starter依赖
移除pom.xml中直接的spring-hateoas和spring-plugin-core依赖,改用Spring Boot提供的starter,它会自动管理适配的版本:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-hateoas</artifactId> </dependency>替换代码中的类引用
检查项目中所有使用SlicedModel的地方,替换为SliceModel,例如:// 旧代码 import org.springframework.hateoas.SlicedModel; // 新代码 import org.springframework.hateoas.SliceModel;清理并重新构建项目
执行以下命令清理本地依赖缓存并重新构建,避免旧版本依赖残留:mvn clean dependency:purge-local-repository install排查依赖冲突
如果仍有异常,执行命令查看依赖树,检查是否有其他依赖引入了旧版本的Spring HATEOAS:mvn dependency:tree找到冲突的依赖后,在对应dependency中添加exclusion排除旧版本:
<dependency> <!-- 冲突的依赖 --> <groupId>xxx</groupId> <artifactId>xxx</artifactId> <exclusions> <exclusion> <groupId>org.springframework.hateoas</groupId> <artifactId>spring-hateoas</artifactId> </exclusion> </exclusions> </dependency>
额外说明
Spring Boot 3.x与Spring HATEOAS 2.x的API有较多变更,除了SlicedModel,还需注意其他已废弃或移除的类/方法,比如PagedResources替换为PageModel,Resources替换为CollectionModel等。
内容的提问来源于stack exchange,提问作者Thorvas
相关产品推荐
相关产品推荐

