Spring Data REST接口404问题:@RepositoryRestResource失效排查
我在用Spring框架时遇到问题:Spring MVC实现的RESTful接口能正常访问,但通过Spring Data REST实现的/posts接口返回404,@RepositoryRestResource注解似乎未生效。已解决javax转jakarta命名空间问题,当前卡在此问题3天。
相关代码
PostRepository.java
package com.example.demo.repository; import com.example.demo.domain.Post; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.query.Param; import org.springframework.data.rest.core.annotation.RepositoryRestResource; import java.util.List; @RepositoryRestResource(collectionResourceRel = "posts", path = "posts") public interface PostRepository extends CrudRepository<Post, Long> { Post findOneByTitle(@Param("title") String title); List<Post> findByBodyContaining(@Param("keyword") String keyword); }
PostRestController.java
package com.example.demo.web; import com.example.demo.domain.Post; import com.example.demo.repository.PostRepository; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class PostRestController { private final PostRepository postRepository; public PostRestController(PostRepository postRepository) { this.postRepository = postRepository; } @RequestMapping(value = "/post/findByTitle") public Post findOneByTitle(@RequestParam("title") String title) { return postRepository.findOneByTitle(title); } @RequestMapping(value = "/post/search") public List<Post> findByBodyContaining(@RequestParam("keyword") String keyword) { return postRepository.findByBodyContaining(keyword); } @RequestMapping(value = "/post/count") public long count() { return postRepository.count(); } }
可正常访问的接口
http://localhost:8080/ http://localhost:8080/h2-console http://localhost:8080/post/search?keyword=Geeky http://localhost:8080/post/findByTitle?title=Youtube
问题现象
访问http://localhost:8080/posts返回404错误。
排查解决方案
确认Spring Data REST依赖存在:Spring Boot项目需引入
spring-boot-starter-data-rest依赖,而非仅spring-boot-starter-data-jpa。
Maven依赖配置:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency>Gradle依赖配置:
implementation 'org.springframework.boot:spring-boot-starter-data-rest'检查实体类Post的JPA注解:确保Post类标注了
@Entity、@Id等必要的JPA注解,Spring Data REST仅会暴露关联JPA实体的Repository。示例:package com.example.demo.domain; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; @Entity public class Post { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String title; private String body; // Getter、Setter及构造方法 }验证Repository扫描范围:主启动类的
@SpringBootApplication默认扫描自身所在包及子包,若PostRepository不在主类的扫描路径下,需手动指定扫描范围,例如:@SpringBootApplication(scanBasePackages = "com.example.demo") @EnableJpaRepositories("com.example.demo.repository") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }检查Spring Data REST路径配置:查看
application.properties或application.yml中是否配置了spring.data.rest.base-path,若设置了前缀(如/api),则访问路径应改为http://localhost:8080/api/posts。查看启动日志:启动项目时,检查日志是否包含类似
Exposing repository com.example.demo.repository.PostRepository under path posts的信息,若没有则说明Repository未被Spring Data REST识别,需排查扫描或依赖问题。
内容的提问来源于stack exchange,提问作者Bhavya Sai Kamasamudram

