2023年9月14日 问题: 解决: 创建好数据库及表 在 spring boot 项目 pom.xml 引入 spring data rest depency <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> <!-- 如加入下边 hal-explorer 则会集成一个类似 swagger ui 一样的接口浏览器,通过接口根目录访问 --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-rest-hal-explorer</artifactId> </dependency> 12345678910 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> <!-- 如加入下边 hal-explorer 则会集成一个类似 swagger ui 一样的接口浏览器,通过接口根目录访问 --><dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-rest-hal-explorer</artifactId> </dependency> 中创建好 Entity Class 创建 repo 如: @RepositoryRestResource(collectionResourceRel = "company", path = "company") public interface CompanyRestRepo extends PagingAndSortingRepository<Company, String>, CrudRepository<Company, String> { Page<Company> findByType(String type, Pageable p);//定制查询方法 可通过 .../company/search/findByType?type=*** 访问 } 123456 @RepositoryRestResource(collectionResourceRel = "company", path = "company")public interface CompanyRestRepo extends PagingAndSortingRepository<Company, String>, CrudRepository<Company, String> { Page<Company> findByType(String type, Pageable p);//定制查询方法 可通过 .../company/search/findByType?type=*** 访问 } 然后就可以 用 rest 的方式交互了。如使用 post json 创建数据; curl --request 'POST' --location 'http://localhost:8080/company' \ --header 'Content-Type: application/json' \ --data '{ "name": "中国石化" }' 12345 curl --request 'POST' --location 'http://localhost:8080/company' \--header 'Content-Type: application/json' \--data '{ "name": "中国石化" }' GET 直接列出数据如: curl --location 'http://localhost:8080/company?page=0&size=20' 1 curl --location 'http://localhost:8080/company?page=0&size=20' curl --location 'http://localhost:8080/company/search/findByType?type=aa&page=0&size=20' 1 curl --location 'http://localhost:8080/company/search/findByType?type=aa&page=0&size=20' ;PUT 修改数据如: curl --location --request PUT 'http://localhost:8080/company/600028' \ --header 'Content-Type: application/json' \ --data '{ "name": "中国石化" }' 12345 curl --location --request PUT 'http://localhost:8080/company/600028' \--header 'Content-Type: application/json' \--data '{ "name": "中国石化"}' end 参考: https://spring.io/guides/gs/accessing-data-rest/ https://docs.spring.io/spring-data/rest/docs/current/reference/html/#upgrading